Monday, April 27, 2009

How to log records to log file? Using log4j

This piece of code will explain the usage of the log4j. In java development, you may have faced for error logging situation. Here is the solution.

Make a log configuration file with follwoing details in your project. Here log configuration file name is "logconfig.txt"

log4j.logger.debugl=INFO,debugl

log4j.appender.debugl.layout=org.apache.log4j.PatternLayout

log4j.appender.debugl=org.apache.log4j.DailyRollingFileAppender

log4j.appender.debugl.File=Log//data_usage_log.txt

log4j.appender.debugl.DatePattern='.'yyyy-MM

log4j.appender.debugl.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}|%m %n


log4j.appender.debugl.File=c:/data_usage_log.txt

There must be a file called "data_usage_log.txt" in C directory. Then it will upgrade with logs.

Add the following methods to your class constructors.

Logger preLogger

constructor(){    


PropertyConfigurator.configure("logconfig.txt");

preLogger = Logger.getLogger("debugl");

}


you can add your error report as follows
preLogger.info(“Logging ”);

No comments:

Post a Comment