. .


ST 4U 212: Customizing log4s output

March 28, 2012 10:40:29.755

Today's Smalltalk 4 You looks at how you can customize the output of log4s data via the built in pattern specifiers - and the support code that you can implement to make use of it. 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 look at customizing your logging (in log4s) via the pattern strings used when setting up a logger. First off, you should look at the documentation, where you'll find this page, explaining the various options:


What we'll do today is set up some object specific logging based on the API supported by our objects. We'll create two simple classes which respond to the same method - #addOne, adding one to a counter. One will implement #printOn: (customizing #printString), the other will implement #printLog4s. To get that all to show up, we'll use the %object specifier in the log setup:

level := EsLogManager getLevel: 'All'.
pattern := EsPatternLayout new: '%date{ISO8601}: [%level]  [%object] %message:'.
transcriptAppender := EsTranscriptAppender level: level layout:  pattern.
logger addAppender: transcriptAppender.

If your object implements #printLog4s, that will get used instead of #printString. Here's what the code for Adder1 and Adder2 (the two classes) looks like:


printOn: stream
	super printOn: stream.
	stream nextPutAll: ' <', count printString, '>'


	^'LOGGING:  <', count printString, '>'

Now we'll execute the following:

"with logging on, go ahead and watch two different results"
adder1 := Adder1 new.
adder2 := Adder2 new.

adder1 addOne.
adder2 addOne.

And this is what we see in the Transcript:

2012-03-28 09:59:22,031: [WARN]  [an Adder1 <1>] Added one:
2012-03-28 09:59:28,937: [WARN]  [LOGGING:  <1>] Added one:

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: , ,

[st4u212-iPhone.m4v ( Size: 4439160 )]

posted by James Robertson

 Share Tweet This