Logging Formatter
A LoggingFormatter implements how an LoggingEvent should be logged, essentially a "printString" representation of the event. Various Formatters are supplied with the system, but it's also easy to rolll your own.
Existing Formatters
SimpleLoggingFormatter
The SimpleLoggingFormatter outputs the level and the message of the event. Its output looks something like this:
DEBUG - Test message
Assign a SimpleLoggingFormatter to to a Logger by using something similar to the following code:
| logger | logger := FileLogger new fileName: 'MyLog.txt'; format: SimpleLoggingFormatter defaultFormat; policy: LoggingPolicy debugPolicy.
TimestampLoggingFormatter
The subclass TimestampLoggingFormatter prepends the timestamp to the aforementioned output. There are four possible timestamp formats you can use.
| Type | Method | Format |
| time | #timeFormat | Formats a Timestamp in the format "HH:mm:ss,SSS" for example, "15:49:37,459". |
| dateTime | #dateTimeFormat | Formats a Timestamp in the format "dd MMM yyyy HH:mm:ss,SSS" for example, "06 Nov 1994 15:49:37,459". |
| ISO8601 | #iso8601Format | Formats a Timestamp in the format "yyyy-MM-dd HH:mm:ss,SSS" for example "1999-11-27 15:49:37,459". |
| elapsed | #elapsedTimeFormat | Formats a Timestamp by printing the number of milliseconds elapsed since construction of the format. |
To instantiate a specific TimestampFormatter, send the method selector (in the second column of the table above) to TimestampLoggingFormatter, e.g.
| logger | logger := FileLogger new fileName: 'MyLog.txt'; format: TimestampLoggingFormatter iso8601Format; policy: LoggingPolicy debugPolicy.TimestampLoggingFormatter defaultFormat is the #timeFormat.
XMLLoggingFormatter
<LoggingEvent loggerName="Transcript" category="debug" level="DEBUG" timestamp="September 13, 2006 7:56:48.001"> <Message>Test message</Message>
</LoggingEvent>
| logger | logger := FileLogger new fileName: 'MyLog.txt'; format: XMLLoggingFormatter defaultFormat; policy: LoggingPolicy debugPolicy.
Implementing your own LoggingFormatter
Implementing your own LoggingFormatter is about as difficult as implementing a #printString method, only this one is called
formatEvent: anEvent on: aStream from: aLogger
By passing both the event and the logger in, we have access not only to all the state of the LoggingEvent, but also to state of the Logger, e.g. the name, or other information we might want to put in an application-spcific logger class we create. Browse implementors of #formatEvent:on:from: for samples.
Depending on what you want your formatter to produce, you may also want to override #outputHeader: and #outputFooter:.
Published Thursday, October 19, 2006
