You are here: Home // Programming // Logging with log4j

Logging with log4j

This article is contributed by Debkumar Basu. He has done an excellent work with Log4J… read it to learn more.

===================================

I was just looking for some cool examples of log4j.properties for a custom application. After some googling and couple of hours of “trial and error”, I came up with this following Log4j.xml.

Have a look, may seem handy to some of you…

The following things are achieved through this file:
1) Enables multiple logger (can be specified on packages )
2) Enables Database logging, Console logging & File Logging
3) Exhibits the additivity property


value=”D:/Log/logger1.log” />
value=”D:/Log/logger2.log” />
value=”D:/Log/logger3.log” />

class=”org.apache.log4j.jdbc.JDBCAppender”>

value=”jdbc:bea:oracle://DBSERVER:PORT;sid=SERVICE ID;user=USER;password=PASSWORSD” />

This configuration is doing the following things:

1) the files which are under a package starting with com are logging to FileAppender and ConsoleAppender.
e.g – com.deb.new.SecurityUrls is logging in FileAppender and ConsoleAppender.
2) The files which are under a package starting with com.common are logging to FileAppender3 only.
e.g. – com.common.security.SecurityEnvelope is logging in FileAppender3 only.
See here, since this file also has com in their package name, they tend to log in FileAppender also.
Hence, we’ve set the  additivity for this logger to false; i.e. the logger won’t be inheriting from its parent “com”.
3) The files which are under a package starting with test are logging to FileAppender2 & DBAppender.
e.g. – test.workflow.edit.StatusController.java is logging in FileAppender2 & DBAppender.
These values “com”, “com.common”, “test” are defined as logger elements in Log4j.xml.
To implement multiple loggers we need to alter the Log4j.xml only. There will be no change in “.java” files.

Blog Widget by LinkWithin

No related posts.

Tags: , , ,

Leave a Reply

Spam Protection by WP-SpamFree

Copyright © 2009 TechCuBeTalk free technology magazine. All rights reserved.
Designed by Theme Junkie. Powered by WordPress.