Sunday, April 8, 2007

Log4J Introduction


In Log4j version 1.2 The Category class is replaced by Logger class
There are 3 components of Log4j are 1. Logger, 2.Appender, 3.Layout
1. Logger - The set of Levels are INFO,DEBUG,FATAL, ERROR,WARN,TRACE
This is the central class in the log4j package. Most logging operations, except configuration, are done through this class.
2. Appender - In log4j the output destination is called the appender
The Appender controls how the logging is output.
ConsoleAppender, WriterAppender, FileAppender, SocketAppender, TelnetAppender
3. Layout - The Appender must have have an associated Layout so it knows how to format the output.
DateLayout, HTMLLayout, PatternLayout, SimpleLayout, XMLLayout
Sample Log4j.properties file
---------------------------------
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

PatternLayout - the Goal of this class is to format the logging event and return the result as String.
%C - Used to output the fully qualified className
%t - Used to Output the Thread Name used to generate the logEvent
%d - Used to output the date of the logging event.
%f - Used to output the filename where the logging event occurs.
%L - Used to output the Line Number from where the logging was issued.
%M - Used to output the Method NAme associated with the logging event.
%p - Used to output the priority of the logging event. more>>>

No comments: