Search This Blog

Tutorials

What is log4j ? How can we implement it? Explain log4j property ?

 

1. Log4j is logging framework developed by Apache
2. There are few steps to implement it into the project .

Step1 : download log4j.jar from apache web site and set into class path .


Step 2 : Create one log4j.properties and set into class path. 

log4j prop file should have following value with naming convention as shown below  :

log4j.rootLogger=ERROR, stdout, logfile // stdout to pring on console  , logfile to print log into file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=logs/sample.log
log4j.appender.logfile.MaxFileSize=5MB
log4j.appender.logfile.MaxBackupIndex=3
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.logger.com.sample=DEBUG


That's it .. Here I have used two type of  appender  , console and file but there lot more also available to configure. Appenders are which helps to print log.

Converiosn patterns Layout decide the pattern of printing . d - date , p - type , c - class name , m - message , n - name

And last line is used for filtering and type according to your packages to make the decision of log printing.
Here under com.sample package all classes debug log message will print if log4j Logger has set into class as following :

private static Logger _log= Logger.getLogger("com.sample.tutorials.main.SpringMain");
_log.debug("test debug");

Note : XML (log4j.xml) is also another way to configure the log4j logger. 

No comments:

Post a Comment

Thanks for your comment, will revert as soon as we read it.

Popular Posts