Steps to follow :
1. Download Log4j Jar file and add in Project Lib Path.
http://logging.apache.org/log4j/1.2/download.html
2. Create Log4j.properties file in your project and add below details.
log4j.rootLogger=DEBUG, Appender1,Appender2
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=applog.txt
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
3. In the main method of Java program add below code.
Logger logger = Logger.getLogger(Conn.class);
String log4jConfigFile = System.getProperty("user.dir") + File.separator + "log4j.properties";
PropertyConfigurator.configure(log4jConfigFile);
logger.debug("this is a debug log message");
logger.info("this is a information log message");
logger.warn("this is a warning log message");
logger.info("This is my first log4j's statement");
At console and applog.txt logs will be created.
Comments
Post a Comment