fwrite 多次保存同一条消息

发布于 2024-10-31 12:30:27 字数 860 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-11-07 12:30:27

我的 fwrite 代码不在任何循环内,因此它肯定只执行一次。使用第三方记录器类也发生了同样的想法。

如果其他人的代码显示相同的行为,那么只有一个可能的根本原因:代码被调用了三次。

可能是时候拿出调试器在代码中添加一个断点,然后单步执行第一次调用后发生的所有事情。这无疑会引导您找到后续调用的来源。

您可能还需要考虑向日志添加完整时间戳和微时间,而不是 getdate。通过证明确实进行了三个调用,这可以使故障排除更加有效。

$time_plus_micro = date('Y-m-d H:i:s') . ' ' . microtime(true)
fwrite($handle, $time_plus_micro . " | ...";

My fwrite code is not inside any loop, so it is definately executed only once. Same think happended using third party logger classes.

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.

$time_plus_micro = date('Y-m-d H:i:s') . ' ' . microtime(true)
fwrite($handle, $time_plus_micro . " | ...";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文