如何使用log4php在日志文件中输出异常信息?
我已经设置 log4php 使用 LoggerAppenderRollingFile 附加程序和 LoggerLayoutTTCC 布局。然而,当我记录异常时,它不会显示异常详细信息,例如堆栈跟踪,就像我习惯在 log4net 中看到的那样。
我快速浏览了代码,它看起来像 LoggerAppenderMongoDB< /a> 支持使用 formatThrowable
方法,但我在其他附加程序中没有看到任何类似的内容。
我觉得我错过了一些明显的东西。我需要配置什么才能将这些详细信息打印到日志文件中吗?我需要创建自定义 LoggerAppender 类吗?或者可以使用不同的布局或自定义渲染器来完成这些?
I have set up log4php to log to a file using the LoggerAppenderRollingFile appender and the LoggerLayoutTTCC layout. When I log an exception, however, it doesn't display the exception details such as the stack trace like I'm used to seeing in log4net.
I've had a quick look through the code and it looks like the LoggerAppenderMongoDB has support for displaying exceptions with the formatThrowable
method, but I don't see anything similar in the other appenders.
I feel like I am missing something obvious. Is there something that I need to configure in order to print these details to the log file? Do I need to create a custom LoggerAppender class? Or can these be done with a different layout or a custom renderer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅供参考,现在
LoggerLayoutTTCC
已弃用,您可以使用 LoggerLayoutPattern 使用更好的格式字符串来包含异常。请参阅文档的“日志记录异常”部分:http://logging.apache。 org/log4php/docs/layouts/pattern.html#Logging_exceptions
FYI, now that
LoggerLayoutTTCC
is deprecated, you can use LoggerLayoutPattern with a better format string to include exceptions.See the Logging Exceptions section of the docs: http://logging.apache.org/log4php/docs/layouts/pattern.html#Logging_exceptions
您应该使用 PHP set_exception_handler 函数并使用此函数包装 log4php。
检查一下: http://php.net/manual/en/ function.set-exception-handler.php
$log->debug($exception->getMessage());
}
set_exception_handler('exception_handler');
You should use PHP set_exception_handler function and wrap log4php using this function.
check it out at: http://php.net/manual/en/function.set-exception-handler.php
$log->debug($exception->getMessage());
}
set_exception_handler('exception_handler');
我得出了同样的结论:看起来 log4php 只使用
LoggerAppenderMongoDB.php
附加程序中的$throwable
参数执行某些操作。LoggerAppenderFile 基类依赖布局 LoggerLayoutTTCC 来格式化消息(如预期的那样)。该类有一个方法
ignoresThrowable()
,它返回 true。尽管这个方法似乎没有被调用,但它确实温和地传达了作者似乎并不打算注意到该错误。我添加了自己的布局类,该类扩展了 LoggerLayoutTTCC 并覆盖 format()
I've come to the same conclusion: it looks like log4php only does something with the
$throwable
parameter in theLoggerAppenderMongoDB.php
appender.The
LoggerAppenderFile
base class relies on the layout,LoggerLayoutTTCC
, to format the message (as would be expected). That class has a methodignoresThrowable()
, which returns true. Although this method does not appear to be called, it does mildly communicate that the authors did not seem to intend to take any note of the error.I have added my own layout class that extends LoggerLayoutTTCC and overrides format()
我花了一段时间才弄清楚为什么抛出异常时 log4php 没有记录原始文件名和行号。事实证明,我的自定义
exception_handler
类仅记录异常消息(通过执行$exception->getMessage()
),其中不包含文件名,也不包含行号。我要做的就是连接该信息:$exception->getFile()
和$exception->getLine()
:不要忘记注册自定义处理程序:
It took me a while to figure out why the original file name and line number was not logged by log4php when an exception is thrown. It turned out that my custom
exception_handler
class was only logging the exception message (by doing$exception->getMessage()
), which does not contain the file name nor the line number. All I to do is concatenate that info:$exception->getFile()
and$exception->getLine()
:Don't forget to register the custom handler: