如何使用log4php在日志文件中输出异常信息?

发布于 2024-11-30 06:13:16 字数 892 浏览 0 评论 0原文

我已经设置 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 技术交流群。

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

发布评论

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

评论(4

北笙凉宸 2024-12-07 06:13:16

仅供参考,现在 LoggerLayoutTTCC 已弃用,您可以使用 LoggerLayoutPattern 使用更好的格式字符串来包含异常。

<layout class="LoggerLayoutPattern">
   <param name="conversionPattern" value="%d{m/d/y H:i:s,u} [%t] %p %c %x - %m %newline%throwable" />
</layout>

<!-- %newline%throwable is the important part -->

请参阅文档的“日志记录异常”部分: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.

<layout class="LoggerLayoutPattern">
   <param name="conversionPattern" value="%d{m/d/y H:i:s,u} [%t] %p %c %x - %m %newline%throwable" />
</layout>

<!-- %newline%throwable is the important part -->

See the Logging Exceptions section of the docs: http://logging.apache.org/log4php/docs/layouts/pattern.html#Logging_exceptions

标点 2024-12-07 06:13:16

您应该使用 PHP set_exception_handler 函数并使用此函数包装 log4php。
检查一下: http://php.net/manual/en/ function.set-exception-handler.php

function exception_handler($exception) {

$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

function exception_handler($exception) {

$log->debug($exception->getMessage());
}

set_exception_handler('exception_handler');

夜夜流光相皎洁 2024-12-07 06:13:16

我得出了同样的结论:看起来 log4php 只使用 LoggerAppenderMongoDB.php 附加程序中的 $throwable 参数执行某些操作。

LoggerAppenderFile 基类依赖布局 LoggerLayoutTTCC 来格式化消息(如预期的那样)。该类有一个方法 ignoresThrowable(),它返回 true。尽管这个方法似乎没有被调用,但它确实温和地传达了作者似乎并不打算注意到该错误。

我添加了自己的布局类,该类扩展了 LoggerLayoutTTCC 并覆盖 format()

    class LoggerLayoutTTCCWithException extends LoggerLayoutTTCC
    {
        public function format(LoggerLoggingEvent $event) {
            $format = parent::format($event);

            $throwableInfo = $event->getThrowableInformation();
            if ($throwableInfo === null) {
                return $format;
            }

            $renderer = new LoggerRendererException();
            return $format . $renderer->render($throwableInfo->getThrowable());
        }
    }

I've come to the same conclusion: it looks like log4php only does something with the $throwable parameter in the LoggerAppenderMongoDB.php appender.

The LoggerAppenderFile base class relies on the layout, LoggerLayoutTTCC, to format the message (as would be expected). That class has a method ignoresThrowable(), 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()

    class LoggerLayoutTTCCWithException extends LoggerLayoutTTCC
    {
        public function format(LoggerLoggingEvent $event) {
            $format = parent::format($event);

            $throwableInfo = $event->getThrowableInformation();
            if ($throwableInfo === null) {
                return $format;
            }

            $renderer = new LoggerRendererException();
            return $format . $renderer->render($throwableInfo->getThrowable());
        }
    }
懷念過去 2024-12-07 06:13:16

我花了一段时间才弄清楚为什么抛出异常时 log4php 没有记录原始文件名和行号。事实证明,我的自定义 exception_handler 类仅记录异常消息(通过执行 $exception->getMessage()),其中不包含文件名,也不包含行号。我要做的就是连接该信息: $exception->getFile()$exception->getLine()

public function exception_handler ($exception) {
        $logger = Logger9::create();
        $logger->info($exception->getMessage()." ".$exception->getFile()." ".$exception->getLine());
}

不要忘记注册自定义处理程序:

@set_exception_handler(array($this, 'exception_handler'));

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():

public function exception_handler ($exception) {
        $logger = Logger9::create();
        $logger->info($exception->getMessage()." ".$exception->getFile()." ".$exception->getLine());
}

Don't forget to register the custom handler:

@set_exception_handler(array($this, 'exception_handler'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文