log4j config




Config log4j  to print log statements on eclipse consle then create a property file and call it as log4j

for ex: log4j.properties

In that file add the below statements

log4j.rootLogger=DEBUG,console,file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=INFO
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n


To write the DEBUG logs to an external file append the below statements to log4j.properties files


# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=AtlasTestExecutionLog.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.Append=false


With the above properties only log.INFO statements prints on console where as log.DEBUG statements write to external file

Note: the line with Threshold will define which debug level needs to print on console and which needs to go into file

that is if the above statement contains Threshold as same as rootLogger then both DEBUG and INFO statements prints on eclipse console


In java file:

import org.apache.log4j.PropertyConfigurator;

and in class use the following statement

......................
......................

protected static Logger log;

......................
......................

log = Logger.getLogger(BaseUITestClass.class);
PropertyConfigurator.configure("log4j.properties");


log4j also can be configurable in xml format



No comments:

Post a Comment