简体   繁体   English

PHP和MySQL中的错误监控

[英]Error monitoring in PHP and MySQL

Are there any reports/logs saying all PHP and MySQL errors that occured on server? 是否有任何报告/日志说明服务器上发生的所有PHP和MySQL错误? With date, time, some user info (browser, IP), and count of that error happened if possible. 使用日期,时间,一些用户信息(浏览器,IP)以及可能发生的错误计数。

In addition every time my mysql_query fails die message is pulled from included file: 另外,每次我的mysql_query失败时,都会从包含的文件中提取消息:

$error_message[0] = 'Error no. 0!';
$error_message[1] = 'Error no. 1!';
$error_message[2] = 'Error no. 2!';

Is there a way to monitor which message and when was displayed? 有没有办法监控哪条消息以及何时显示?

Yes, there are error logs for both: httpd (or apache) and mysqld. 是的,两者都有错误日志:httpd(或apache)和mysqld。

You find them in: 你找到他们:

/var/log/httpd/error_log
/var/log/mysqld.log

(these path could be a little different on your sever depends on configuration files, eg. you can specify mysql error log file location passing --log-error=XXX option to mysqld) (这些路径在您的服务器上可能会略有不同取决于配置文件,例如,您可以指定mysql错误日志文件位置传递--log-error = XXX选项到mysqld)

You can do it by sticking to exceptions instead of "die()" calls, and by catching them, then logging. 您可以通过坚持异常而不是“die()”调用,捕获它们,然后记录来实现。

Good solution for that is eg. 对此的良好解决方案是。 Sentry - it saves information in the way you require (time of occurrence, details, number of occurences, logger/category, level etc.). Sentry - 它以您需要的方式保存信息(发生时间,详细信息,出现次数,记录器/类别,级别等)。

查看/var/logs/apache//var/logs/httpd/如果它是Linux / UNIX服务器。

"Is there a way to monitor which message and when was displayed?" “有没有办法监控哪条消息以及何时显示?”

This is not about the main question, however if you're worried about exposed error messages, you can simply disable showing errors in PHP, which actually means you would not have MySQL errors showing to the visitors also (You're working on MySQL via PHP, if you're talking about a web application) 这不是关于主要问题,但是如果您担心暴露的错误消息,您可以简单地禁用在PHP中显示错误,这实际上意味着您也不会向访问者显示MySQL错误(您正在通过MySQL工作) PHP,如果你在谈论一个Web应用程序)

That might help! 这可能有所帮助! :) :)

Do you think try, catch would works then? 你觉得try, catch会起作用吗? you just need to use it everywhere in order to catch all the possible errors. 你只需要在任何地方使用它来捕获所有可能的错误。

You may also make your own MySQL adapter class and then wrap the original functions inside the try, catch block. 您也可以创建自己的MySQL适配器类,然后将原始函数包装在try, catch块中。 That would make it easier if you could apply that on your code, but for PHP errors, still need to cover fatal-error-possible parts, eg working on file system, with try, catch blocks manually. 如果您可以将它应用于您的代码,那将更容易,但对于PHP错误,仍然需要覆盖致命错误可能的部分,例如在文件系统上工作try, catch手动使用try, catch块。

When you're gonna log them on the database or file system, obviously you can log the date and time also. 当您要将它们记录在数据库或文件系统上时,显然您也可以记录日期和时间。

PHP manual/Exceptions PHP手册/例外

Note: you may consider the overhead also! 注意:您也可以考虑开销! you can benchmark to see how much your code would be slower after using try, catch blocks. 你可以通过基准测试来看看在使用try, catch块之后你的代码会变慢多少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM