Technical Tips, Tricks and Articles

Posts Tagged ‘properties

Quickstart – log4j

leave a comment »

There are like 100 of log4j tutorials on the internet. I am not going to put another one, just a compilation of few resources which are good to go and will add couple of notes, which I wasn’t able to find online or they took forever.

This particular link is pretty decent. It talks about basics, MulitpleAppenders, FileAppenders and then log4j xml configurations.

One of the thing that is missed in above tutorial is configuring your XML based configurations for specific class hierarchy. Lets talk about vaanila.admin and vaanila.report class heirarchy mentioned in FileAppenders  example. In given example, following piece of lines were handling specific logging based on classes in .properties file

log4j.logger.com.vaannila.admin=WARN,AdminFileAppender
log4j.logger.com.vaannila.report=DEBUG,ReportFileAppender

What would be their equvialent in XML ?

01 <logger name="vaanila.admin">
02 	<level value="WARN"/>
03 	<appender-ref ref="AdminFileAppender"/>
04 </logger>

and similarly, for vaanila.report the configuration would be

01 <logger name="vaanila.report">
02 	<level value="WARN"/>
03 	<appender-ref ref="ReportFileAppender"/>
04 </logger>

One of the thing, that these tutorials didn’t help me was how can I log specific logging based on application modules i.e. if I have multiple .ear files, then how can I separate the loggings for each .ear file? Definitely, the class level logging wouldn’t help me because there could be a scenario where there are “shared” classes. The solution to this question is log4j version 3.2.4 and higher. Using that log4j you can use following configuration to log based on ear files.

Assuming I have an application and one of the ear module is named as some.ear. The settings would look like something

01 <appender name="some" class="org.apache.log4j.FileAppender">
02 	<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"></errorHandler>
03 	<param name="Append" value="false"/>
04 	<param name="File" value="./log/some.log"/>
05 	<layout class="org.apache.log4j.PatternLayout">
06 		<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
07 	</layout>
08 	<filter class="org.jboss.logging.filter.TCLFilter">
09 		<param name="AcceptOnMatch" value="true"/>
10 		<param name="DeployURL" value="some.ear"/>
11 	</filter>
12 </appender>
13 
14 ...
15 
16 <root>
17 	<appender-ref ref="CONSOLE"></appender-ref>
18 	<appender-ref ref="FILE"></appender-ref>
19 	<appender-ref ref="some"></appender-ref>
20 </root>

I hope that will be helpful.

Written by ..alee

April 29, 2011 at 4:12 pm