. .


ST 4U 210: Filtering Logs by Level

March 23, 2012 9:33:19.442

Today's Smalltalk 4 You looks at filtering logging events in log4s using logging levels. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:


If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:

Today we'll take a closer look at logging levels in log4s. To quote the documentation in VA Smalltalk:

The concept of levels is central to log4s. A loggingEvent will be logged by an Appender if the level of the loggingEvent is greater than or equal to the level of the Appender. Both LoggingEvents and Appenders have levels. Log4s has seven predefined levels which are implemented as EsLevel class variables.

There are 7 levels to logging - in order from the lowest (everything gets logged) to the highest (nothing gets logged), they are: All, Debug, Info, Warn, Error, Fatal, Off. So if you set the level for an appender to "Error", and then send a "Warn" level message, it will not appear in that appender's stream. For example - recall that we've set up the root logger with a file appender:

dailyRollingFileAppender =(fileAppender, root, vaLog.log, false, Info, EsPatternLayout,
 'ڴe{ISO8601}: [%level] %message', true, topOfDay )

That will log everything to a file. Let's set up a new appender to the Transcript, but set it up at the "Error" level - meaning, it will ignore all logging events under that:

"Add a transcript appender programmatically, but set at a higher level"
level := EsLogManager getLevel: 'Error'.
pattern := EsPatternLayout new: 'ڴe{ISO8601}: [%level] %message'.
transcriptAppender := EsTranscriptAppender level: level layout:  pattern.
logger addAppender: transcriptAppender.

Now we have two appenders for the default logger - the file appender will get every logging event, while the Transcript will only see things at level "Error" and above. To demonstrate, let's send a warning event:

EsLogManager warn: 'This is a warning!'.

The File log has that, while the Transcript is blank:


Now let's send an error level event, and look again:

EsLogManager error: 'This is an Error!'.


Notice that this time, the event hit both logs - the file appender gets everything, while the Transcript appender got the higher level event only. Next time, we'll get into filtering and the print format.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Technorati Tags: , , ,

[st4u210-iPhone.m4v ( Size: 5721668 )]

posted by James Robertson

 Share Tweet This