LoggerLayoutPatternLoggerLayoutPattern is a flexible layout configurable via a conversion pattern. ParametersThe following parameters are available:
Conversion patternsThis is the string which controls formatting and consists of a mix of literal content and conversion specifiers. The conversion pattern is closely related to the conversion pattern of the printf function in C. It is composed of literal text and format control expressions called conversion specifiers. You are free to insert any literal text within the conversion pattern. Each conversion specifier starts with a percent sign (%) and is followed by optional format modifiers and a conversion character. 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. All format modifiers are optional, and are placed between the percent sign and the conversion character. The first format modifier is the left justification flag which is just the minus (-) character. Then comes the >minimum field width modifier. This is an integer that represents the minimum number of characters to output. If the data item requires fewer characters, it is padded on either the left or the right until the minimum width is reached. The default is to pad on the left (right justify) but you can specify right padding with the left justification flag. The padding character is space. If the data item is larger than the minimum field width, the field is expanded to accommodate the data. The value is never truncated. This behavior can be changed using the maximum field width modifier which is designated by a period (.) followed by an integer. If the data item is longer than the maximum field, then the extra characters are removed from the beginning of the data item and not from the end. For example, it the maximum field width is eight and the data item is ten characters long, then the first two characters of the data item are dropped. This behavior deviates from the printf function in C where truncation is done from the end. The following table demonstrates various uses of format modifiers:
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. Save the configuration to a file called layout_pattern.xml.
<configuration xmlns="http://logging.apache.org/log4php/"> <appender name="default" class="LoggerAppenderEcho"> <layout class="LoggerLayoutPattern"> <param name="conversionPattern" value="%d{Y-m-d H:i:s.u} %c %-5p %m%n" /> </layout> </appender> <root> <appender_ref ref="default" /> </root> </configuration> array( 'appenders' => array( 'default' => array( 'class' => 'LoggerAppenderEcho', 'layout' => array( 'class' => 'LoggerLayoutPattern', 'params' => array( 'conversionPattern' => '%d{Y-m-d H:i:s.u} %c %-5p %m%n' ) ) ) ), 'rootLogger' => array( 'appenders' => array('default') ), ) Run the following code: Logger::configure("layout_pattern.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."); Pattern exampleConversion pattern: %d %c %-5p %m%n Produces the following output: 2011-09-28 09:29:38,602 myLogger INFO Lorem ipsum dolor sit amet, consectetur adipiscing elit. 2011-09-28 09:29:38,603 myLogger DEBUG Donec a diam lectus. 2011-09-28 09:29:38,604 myLogger WARN Sed sit amet ipsum mauris. In this example, %d produces the event datetime in default format (Y-m-d H:i:s,u), and %-5p 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. |