PHP中全局错误处理的最佳做法? [英] Best practices for global error handling in PHP?

查看:113
本文介绍了PHP中全局错误处理的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个类将错误转换为PHP 5中的异常,并将错误记录到文件和/或将其发送到指定的帐户。有没有更好的方法来做到这一点?有一些这样我知道可以更好。我正在使用set_error_handler。

  set_error_handler(exception_error_handler); 

我的代码做了它应该在它记录和电子邮件错误,但我做的最好的过程办法。将它更好地记录到数据库 - 假设数据连接将出现在错误中。

解决方案

您处理错误的代码必须是绝对防弹的。



有时候,由于你忘了测试一个真正的晦涩的原因,它会踢,但是当它挣扎通过代码版本的启示。



将其输出写入数据库为您的代码创建了巨大的依赖关系,而且数据库的缺失很可能是将被报告的主要问题。



依靠邮件仍然是依赖关系,但是在发生中断时最直接的目标应该是让系统再次运行 - 所以发送电子邮件是非常有效的方式提醒您需要修复某些东西。



PHP的文件处理设施不适合并发访问 - 所以尽管我建议在本地记录任何事件,不要从代码中写入文件 - 使用syslog界面。一般来说,发送一封电子邮件,其中包含之后相关详细信息发送到syslog。



HTH



C。


I used a class that converts errors to exceptions in PHP 5 and it logs the errors to a file and/or emails them to a specified account. Is there a better way to do this? There is something about this I know can be better. I am using set_error_handler.

set_error_handler("exception_error_handler");

My code does what it should in that it logs and emails errors but am I doing the process the best way. Would it be better to log it to a Database - assuming a data connection would be present in an error. What is the industry standard for web sites?

解决方案

Your code for dealing with errors must be absolutely bulletproof.

Sometimes it will kick in because of a really obscure reason that you forgot to test for, but you still want it to run when when its struggling through the code version of the apocalypse.

Writing its output to a database creates a huge dependency for you code - and the absence of the database is most likely to be a major cause of problems which would be reported.

Relying on mail is still a dependency, however the most immediate objective in the event of an outage should be to get the system working again - so sending an email is a very effective way of alerting you that you need to fix something.

PHP's file handling facilities do not lend themselves to concurrent access - so although I'd recommend logging any events locally, do not write the files from your code - use the syslog interface. By all means send an email with the relevant details after you've sent it to the syslog.

HTH

C.

这篇关于PHP中全局错误处理的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆