How to output exception information to a log file using log4php?

I set log4php to enter the file using the LoggerAppenderRollingFile and LoggerLayoutTTCC layout. However, when I register an exception, it does not display exception information, such as a stack trace, as I used to see in log4net.

I quickly looked through the code and it looks like LoggerAppenderMongoDB supports displaying exceptions using formatThrowable , but I don't see anything like it in other applications.

I feel like I'm missing something obvious. Is there something I need to set up to print this data in a log file? Do I need to create my own LoggerAppender class? Or can this be done using another layout or a custom renderer?

+8
php logging exception log4php
source share
4 answers

FYI, now that LoggerLayoutTTCC deprecated, you can use LoggerLayoutPattern with the best 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 "Log Exclusions" section in the docs: http://logging.apache.org/log4php/docs/layouts/pattern.html#Logging_exceptions

+2
source share

You should use the PHP function set_exception_handler and wrap log4php using this function. check it out: http://php.net/manual/en/function.set-exception-handler.php

 function exception_handler($exception) { 

$ log-> debugging ($ exception-> GetMessage ()); }

set_exception_handler ('exception_handler');

+1
source share

I came to the same conclusion: it looks like log4php is only doing something with the $throwable parameter in the app LoggerAppenderMongoDB.php .

The base class LoggerAppenderFile relies on the layout, LoggerLayoutTTCC , to format the message (as you would expect). This class has an ignoresThrowable() method that returns true. Although this method does not appear to be called, it explicitly states that the authors do not seem to intend to make any comments about the error.

I added my own layout class, which extends LoggerLayoutTTCC and overrides the 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()); } } 
+1
source share

It took me a while to figure out why the original file name and line number were not written to log4php when an exception occurred. It turned out that my custom class exception_handler only logged an exception message (by doing $exception->getMessage() ), which did not contain either a file name or a line number. All I need to do is combine this information: $exception->getFile() and $exception->getLine() :

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

Remember to register a custom handler:

 @set_exception_handler(array($this, 'exception_handler')); 
0
source share

All Articles