Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output exception information in log file with log4php?

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?

like image 588
g . Avatar asked Aug 18 '11 12:08

g .


2 Answers

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

like image 129
takteek Avatar answered Nov 15 '22 14:11

takteek


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');

like image 1
Itamar Lavender Avatar answered Nov 15 '22 15:11

Itamar Lavender