Friday, 19 June 2009

Java :Add Logger system

1. download log4j.jar file
2. Add into our Java project libraries
3. Create new properties file at root with name "log4j.properties" and copy and paste following code

# Log levels
# Uncomment the following line to enable full loggin for every class
#log4j.rootLogger=trace, stdout, R
log4j.logger.gr.xfrag=trace, stdout, R

# Console appender configuration
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file.
log4j.appender.R.File=./logs/applog.log
log4j.appender.R.MaxFileSize=500KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
# Rolling File Appender layout
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n

4. Add following code on class/web-page,

private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(this.getClass());
log.setLevel(org.apache.log4j.Level.ALL);
log.debug( request.getRemoteAddr() + " This is Index page log ");

Note: request.getRemoteAddr() gives IP address where web page is called.

It stores the log file on 'C:\Program Files\Apache Software Foundation\Tomcat 6.0' with Demo.log file, creates all new logs with following formate
2009-06-06 20:29:02,562 - org.apache.jsp.index_jsp - DEBUG - 0:0:0:0:0:0:0:1 This is Index page log

If you want to use GUI to read logger files then use LogMX(v1.3.2).
1. Create new formate parser. Goto Tools > Option.
2. It opens new option window, Under Parsers tags create new parser formate .
3. It shows new 'Add a log file parser' window, under log4j pattern parser add new patter which is used in "log4j.properties".
4. It parse your logger file.

0 comments:

Post a Comment