简体   繁体   English

更改PHP error_log行为可能吗?

[英]Changing PHP error_log behaviour possible?

I'm wondering if it's possible to change the default behaviour of PHP's error_log() functionality to include the source and originating line number by default. 我想知道是否可以更改PHP的error_log()功能的默认行为,使其默认包含源行和原始行号。

I am guessing this could be potentially be done either with a PHP ini setting or by configuring apache in some specific way, though not being an apache guru I'm not sure how this would be made possible. 我猜这可能可以通过PHP ini设置或通过以某种特定方式配置apache来完成,尽管不是apache专家,但我不确定这将如何实现。

In my default environment, finding the source of the errors in a log generally doesn't pose too much of a problem where the error is legitimately spawned by a PHP warning or notice, as it will report automatically where the line originates... however, calls made manually by programmers to PHP error_log() don't do this, and I can't find a way to make this behavior default. 在我的默认环境中,在错误由PHP警告或通知合法产生的情况下,在日志中查找错误的来源通常不会造成太大的问题,因为它将自动报告行的起源...但是,由程序员手动调用PHP error_log()不会执行此操作,并且我找不到将这种行为设为默认行为的方法。

I am aware that generally speaking you can achieve the line reporting manually with the magic constant like this: 我知道一般来说,您可以使用如下所示的魔术常数手动实现行报告:

error_log("Failed to login to MySQL ".__LINE__);

However, I am curious and open to suggestions about if there are any ways to perhaps configure the way errors are reported universally in the log or anything else to get around changing every single call in code to include the magic constant. 但是,对于是否有任何方法可以配置错误在日志中普遍报告的方式或其他任何方法来解决更改代码中的每个调用以包括魔术常数的问题,我感到好奇并乐于接受。

You can define your own error handler using the set_error_handler function. 您可以使用set_error_handler函数定义自己的错误处理程序。 This allows you to do with the errors whatever you like, as long as it is within the scope of PHP language (including printing the file and line where the error occured). 只要您在PHP语言的范围内(包括打印发生错误的文件和行),就可以使用您喜欢的错误进行处理。 Most common course of action in this case is to convert the error into a ErrorException . 在这种情况下,最常见的做法是将错误转换为ErrorException

Also you can use tools like xDebug , which, when activated in php.ini, display the errors in more readable form. 您还可以使用xDebug之类的工具,该工具在php.ini中激活后,将以更易读的形式显示错误。

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

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