LoggerLayoutPatternLoggerLayoutPattern is a flexible layout configurable via a conversion pattern. ParametersThe following parameters are available:
Conversion patternsConversion pattern is a string which controls the formatting of logging events. It controls how logging events will be transformed into strings by the layout. The conversion pattern is closely related to the conversion pattern of the PHP sprintf function. It is composed of literal text and format control expressions called conversion specifiers. A conversion specifier begins with a percent sign (%) and is followed by a conversion word. Some conversion words require one or more options to be given. These are specified in braces after the conversion word. An example of a conversion specifier is %message which will be converted into the message from the logging event which is being logged. The recognized conversion specifiers are:
Format modifiersBy default the relevant information is output as-is. However, with the aid of format modifiers it is possible to change the minimum and maximum width and the justifications of each data field. Both format modifiers are optional, and are placed between the percent sign (%) and the conversion word. These are, in order:
The following table demonstrates various uses of format modifiers:
The following table lists a couple of examples for using format modifiers. Note that the square brackets are added to the conversion pattern to delimit the output.
ExamplesThe following configuration configures a LoggerAppenderEcho which uses the pattern layout. All examples will use the same code and configuration, only the conversion pattern will change from example to example.
<configuration xmlns="http://logging.apache.org/log4php/">
<appender name="default" class="LoggerAppenderEcho">
<layout class="LoggerLayoutPattern">
<param name="conversionPattern" value="%date %logger %-5level %msg%n" />
</layout>
</appender>
<root>
<appender_ref ref="default" />
</root>
</configuration>
array(
'appenders' => array(
'default' => array(
'class' => 'LoggerAppenderEcho',
'layout' => array(
'class' => 'LoggerLayoutPattern',
'params' => array(
'conversionPattern' => '%date %logger %-5level %msg%n'
)
)
)
),
'rootLogger' => array(
'appenders' => array('default')
),
)
Example code:
Logger::configure("config.xml");
$logger = Logger::getLogger('myLogger');
$logger->info("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
$logger->debug("Donec a diam lectus.");
$logger->warn("Sed sit amet ipsum mauris.");
A simple exampleConversion pattern: %date %logger %-5level %msg%n Running the example code produces the following output: 2012-02-27T19:42:17+01:00 myLogger INFO Lorem ipsum dolor sit amet, consectetur adipiscing elit. 2012-02-27T19:42:17+01:00 myLogger DEBUG Donec a diam lectus. 2012-02-27T19:42:17+01:00 myLogger WARN Sed sit amet ipsum mauris. In this example, %date is converted to the event datetime in default format (corresponding to the ISO-8601 specification), and %-5level produces the event level right padded to 5 characters. Since longest level name is 5 characters long, this ensures that the message always starts at the same character position which improves log readability. Notice that the newline between logging events (%n) has to be explicitely defined. Otherwise all logging events will be logged in the same line. Formatting the dateThe %date conversion word can take the desired date format as an option. For example, if you're European, the d.m.Y date format might be more familiar. Also, adding milliseconds. Conversion pattern: %date{d.m.Y H:i:s,u} %logger %-5level %msg%n Running the example code produces the following output: 27.02.2012 20:14:41,624 myLogger INFO Lorem ipsum dolor sit amet, consectetur adipiscing elit. 27.02.2012 20:14:41,625 myLogger DEBUG Donec a diam lectus. 27.02.2012 20:14:41,626 myLogger WARN Sed sit amet ipsum mauris. Logging HTTP requestsIf log4php is used to log HTTP requests, a pattern like this might be useful: %date [%pid] From:%server{REMOTE_ADDR}:%server{REMOTE_PORT} Request:[%request] Message: %msg%n Request /test.php?foo=bar it will produce the output similar to: 2012-01-02T14:19:33+01:00 [22924] From:194.152.205.71:11257 Request:[foo=bar] Message: Lorem ipsum dolor sit amet, consectetur adipiscing elit. 2012-01-02T14:19:33+01:00 [22924] From:194.152.205.71:11257 Request:[foo=bar] Message: Donec a diam lectus. 2012-01-02T14:19:33+01:00 [22924] From:194.152.205.71:11257 Request:[foo=bar] Message: Sed sit amet ipsum mauris. %server{REMOTE_ADDR} is equivalent to PHP code $_SERVER['REMOTE_ADDR']. Logging exceptionsIf you wish to log any exception passed to the logging methods, you should add the %ex specifier to the end of your conversion pattern, after %newline. This way, if an exception is loggerd it will be addded to your log below your message. For example: %date %logger %message%newline%ex In the following code, suppose that the work() method can throw an exception. This wolud be a good way to deal with it:
$log = Logger::getLogger('foo');
$log->info("Let's try this");
try
{
$foo = new Foo();
$foo->work(123);
}
catch(Exception $ex)
{
// Exception is passed as the second parameter
$log->error("That didn't work", $ex);
}
$log->info("Done.");
If work() throws an exception, your log might look something like this:
2012-10-08T10:11:18+02:00 foo Let's try this
2012-10-08T10:11:18+02:00 foo That didn't work
exception 'Exception' with message 'Doesn't work' in D:\work\exceptions.php:38
Stack trace:
#0 D:\work\exceptions.php(29): Bar->work(123)
#1 D:\work\exceptions.php(48): Foo->work(123)
#2 {main}
2012-10-08T10:11:18+02:00 foo Done.
The exception, along with the full stack trace ends up in your log. This also works with nested exceptions, the full stack trace is added. |