Specifying the Log Entry
Each Appender must have a layout which is an instance of the class EsPatternLayout or one of its sub classes. Each layout object has a pattern which is a string that describes what parts of the EsLoggingEvent object should be logged as well as how they should be formatted into a string before being output by the Appender.
The pattern string consists of conversion specifiers and literals in any order. For example, '%m' logs only the EsLoggingEvent message.
Log4s does not support all the conversion specifiers that Log4j does; for instance, there is no line number specifier because Smalltalk does not have line numbers. Conversion specifiers are mapped to EsPatternConverter objects which do the actual formatting work. The mapping table is in EsPatternParser class>> initializePatternLayoutRules.
A conversion specifier that is new in log4s is the '%o' or '%object' specifier, which maps to the EsObjectPatternConverter class. This specifer attempts to mimic the object rendering in Log4j, where you can specify an object that will render another object in a custom format.
The EsObjectPatternConverter asks the object held in the EsLoggingEvent instance variable object if it responds to the message printLog4s; if so, it sends the object instance variable the printLog4s message; if not the EsObjectPatternConverter sends the object instance variable the printString message. For example, if the object instance variable is an instance of class Foo, creating a #printLog4s method in class Foo will customize its output when using this conversion specifier.
Conversion Specifiers
The following patterns specify what information will be included in a log entry:
• %c or %logger outputs the EsLogger name, e.g., ‘root’.
• %C or %class outputs the name of the class of the method that initiated the logging, e.g., ‘Foo’.
• %d or %date by itself will log the date as ‘28 Aug 1989 08:15:07,000’. Log4s has additional date and time formatting described below.
• %l outputs the name of both the class and the method that initiated the logging, e.g., ‘Foo>>bar’.
• %m or %message outputs the EsLoggingEvent message, e.g., 'Hello, world'.
• %M or %method outputs the name of the method that initiated the logging, e.g., ‘bar’.
• %n outputs the platform dependent line separator character or characters, i.e., line feed or carriage return and line feed.
• %o or %object - if the class of the object instance variable of the EsLoggingEvent respondsTo: #printLog4s, then it will be called. Otherwise, printString will be called.
• %p or %level outputs the name of the level of the EsLoggingEvent, e.g., ‘WARN’.
• %r or %relative outputs the relative time that has elapsed since the application was started, e.g., ‘2:34:01:31.195’
• %t or %thread outputs the name of the current process, e.g., ‘(8/9/2011 1:21:02 PM)’
Patterns can contain literals, too. For example, ‘[%c]’ outputs the EsLogger name in square brackets, e.g., '[root]'
Formatting the Output
Format modifiers can optionally be added to the pattern string to change the minimum and maximum field width and to control justification. The format modifier character is placed between the percent sign and the conversion specifier.
The left justification flag is the minus (-) character, e.g. '%-m'.
The minimum field width modifier is a decimal constant that specifies the minimum number of characters to output, e.g., '%15m'. If the data is smaller than the minimum field width, then it will be padded on the left with spaces, unless the left justification flag is specified. The data will not be truncated if it is larger than the minimum field width.
The maximum field width modifier is a period followed by a decimal constant that specifies the maximum number of characters to output, e.g. '%.20m'. If the data is longer than the maximum field width, then the extra characters are removed from the beginning of the data, not the end.
| | | | |
%20m | false | 20 | none | Left pad with spaces if the message is less than 20 characters long. |
%-20m | true | 20 | none | Right pad with spaces if the message is less than 20 characters long. |
%.30m | n/a | none | 30 | Truncate from the beginning if the message is longer than 30 characters. |
%20.30m | false | 20 | 30 | Left pad with spaces if the message is less than 20 characters long. Truncate from the beginning if the message is longer than 30 characters. |
Date and Time Formatting
The date and time formatting in log4s is similar to that in log4j.
The pattern '%d' will print the date of the EsLoggingEvent in the format dd MMM yyyy HH:mm:ss,SSS, e.g., 28 Aug 1989 08:15:07,000.
Date and time formatting is done by the EsDatePatternConverter class. Formatting options can be passed to the class by enclosing them in curly braces {} after the %d pattern specifier, e.g., '%d{dd MMM yyyy HH:mm:ss,SSS}'.
In addition to using a custom formatting option, you can also use one of these predefined aliases:
'%d{DATE}' : dd MMM yyyy HH:mm:ss,SSS, e.g., 28 Aug 1989 08:15:07,000
'%d{ABSOLUTE}' : HH:mm:ss,SSS, e.g., 08:15:07,000
'%d{ISO8601}' : yyyy-MM-dd HH:mm:ss,SSS, e.g., 1989-08-28 08:15:07,000
The following table describes the date formatting options available in log4s.
| | | | |
2-digit year | y | %y | 2011 | 11 |
year | yyyy | %Y | 2011 | 2011 |
Month with no leading zeros | M | %z | 07 | 7 |
month | MM | %m | 07 | 07 |
Month short name | MMM | %b | 07 | Jul |
Month long name | MMMM+ | %B | 07 | July |
day of month | dd | %d | 05 | 05 |
day of month with leading space | d | %e | 05 | space 5 |
day of month with no leading zeros | f | %f | 05 | 5 |
day of week in month (1-7), Monday=1 | u | %u | 8-07-2011 | 7 |
day of week (0-6) in month, Sunday = 0 | w | %w | 8-07-2011 | 0 |
day of year | D | %j | 8-17-2011 | 229 |
day of week short name | E | %a | 8-15-2011 | Mon |
day of week short name | EE | %a | 8-15-2011 | Mon |
day of week short name | EEE | %a | 8-15-2011 | Mon |
day of week long name | EEEE+ | %A | 8-15-2011 | Monday |
The following table describes the time formatting options available in log4s.
| | | | |
am/pm | a | %P | 08:15:07 | am |
hour in day (0-23), no leading zeros | H | %k | 08:15:07 | 8 |
Hour in day (0-23) | HH | %H | 08:15:07 | 08 |
hour in am/pm (1-12), no leading zeros | h | %l (small l) | 18:15:07 | 6 |
hour in am/pm (1-12) | hh | %I (capital I) | 18:15:07 | 06 |
minutes | mm | %M | 08:15:07 | 15 |
seconds | ss | %S | 08:15:07 | 07 |
milliseconds | SSS | %_ | 08:15:07,017 | 017 |
Date and Time Formatting Examples
| | Output |
8-28-1989 8:15:07 AM | %d | 28 Aug 1989 08:15:07,000 |
8-28-1989 8:15:07 AM | %d{yyyy-MM-dd} | 1989-08-28 |
8-07-1989 8:15:07 AM | %d{M-f-yyyy} | 8-7-1989 |
8-07-1989 8:15:07 AM | %d{M-d-yyyy} | 8- 7-1989 |
8-28-1989 8:15:07 AM | %d{dd MMM yyyy HH:mm:ss a} | 8-28-1989 8:15:07 am |
8-28-1989 8:15:07 AM | %d{yyyy_MMM_dd EE HH:mm:ss} | 1989_Aug_28 Mon 08:15:07 |
8-28-1989 8:15:07 AM | %d{yyyy_MMM_dd EEEE HH:mm:ss} | 1989_Aug_28 Monday 08:15:07 |
8-28-1989 8:15:07 AM | %d(D) | 240 |
8-28-1989 8:15:07 AM | %d{HH:mm:ss,SSS} | 20:15:07,000 |
8-28-1989 8:15:07 AM | %d{yyyy-MMM-dd} | 1989-Aug-28 |
8-28-1989 8:15:07 AM | %d{yyyy-MMMM-dd} | 1989-August-28 |
8-28-1989 8:15:07 AM | %d{yy-MMM-dd} | 89-Aug-28 |
8-28-1989 8:15:07 AM | %d{-->yy-MMM-dd<--} | -->89-Aug-28<-- |