fwrite 多次保存同一条消息
我在 php 中使用 fwrite 在 messages.txt 中记录一些错误。
问题是,当我打开 db_errors.txt 时,我看到该消息保存了多次而不是一次。
我的 fwrite 代码不在任何循环内,因此它肯定只执行一次。使用第三方记录器类也发生了同样的想法。
if (!$result = mysqli_query($link, $query)){
$today = getdate();
$handle = fopen("logs/db_errors.txt", "a");
fwrite($handle, $today['mday'].'/'.$today['mon'].'/'.$today['year']." | ".mysqli_errno($link)." : ".mysqli_error($link)." | ".$query." \n");
fclose($handle);
}
这会在 db_errors.txt 中写入 3 行,并具有相同的输出。
11/4/2011 | 1054 : Unknown column 'uids' in 'field list' | SELECT uids FROM users WHERE user_id=6 LIMIT 1
11/4/2011 | 1054 : Unknown column 'uids' in 'field list' | SELECT uids FROM users WHERE user_id=6 LIMIT 1
11/4/2011 | 1054 : Unknown column 'uids' in 'field list' | SELECT uids FROM users WHERE user_id=6 LIMIT 1
I use fwrite in php to log some errors in logs.txt.
The problem is that when I open db_errors.txt I see the message saved multiple times instead of just once.
My fwrite code is not inside any loop, so it is definately executed only once. Same think happended using third party logger classes.
if (!$result = mysqli_query($link, $query)){
$today = getdate();
$handle = fopen("logs/db_errors.txt", "a");
fwrite($handle, $today['mday'].'/'.$today['mon'].'/'.$today['year']." | ".mysqli_errno($link)." : ".mysqli_error($link)." | ".$query." \n");
fclose($handle);
}
this writes in 3 lines inside db_errors.txt with the same output.
11/4/2011 | 1054 : Unknown column 'uids' in 'field list' | SELECT uids FROM users WHERE user_id=6 LIMIT 1
11/4/2011 | 1054 : Unknown column 'uids' in 'field list' | SELECT uids FROM users WHERE user_id=6 LIMIT 1
11/4/2011 | 1054 : Unknown column 'uids' in 'field list' | SELECT uids FROM users WHERE user_id=6 LIMIT 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果其他人的代码显示相同的行为,那么只有一个可能的根本原因:代码被调用了三次。
可能是时候拿出调试器和在代码中添加一个断点,然后单步执行第一次调用后发生的所有事情。这无疑会引导您找到后续调用的来源。
您可能还需要考虑向日志添加完整时间戳和微时间,而不是
getdate
。通过证明确实进行了三个调用,这可以使故障排除更加有效。If someone else's code shows the same behavior, then you have only one possible root cause: The code is being called three times.
It's probably time to get out a debugger and add a breakpoint to your code, then step through everything that happens after the first call. This will undoubtedly lead you to the source of the later calls.
You might also want to consider adding a full timestamp and a microtime to the log instead of
getdate
. This may make troubleshooting more effective by proving that there are indeed three calls being made.