Saturday, 26 January 2013

Basic introduction and usage of log4j

Log4j is used to log the data in a format that will be understandable and useful. Here is the documentation. 

I am going to explain how to start a simple logger using log4j for you project with a basic example. 


Download the file from Apache log4j.
  1. Create a project. (I have created a java project).
  2. Create a file with name log4j.properties(Now it can be log4j.xml also). 
  3. If it is a java project, copy properties file into src folder.
  4. If it is a web project then copy this project under WEB-INF/classes
  5. Add following entries to the file. (You can add more entries, see. Documentation)
# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout(console)
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


Run your project now and check your log file/ console.....
 

No comments:

Post a Comment