Pages

09 December 2008

eclipse and log4j FileAppender

I have a webapp named 2log4j running on Tomcat 5.5, which requires me to do all logging using Log4j. This is the file structure in eclipse package explorer panel.

From Drop Box

Here are my log4j.properties file and java class that do the logging.

log4j.properties
### direct log messages to stdout ###

#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.out

log4j.appender.stdout=org.apache.log4j.RollingFileAppender
log4j.appender.stdout.File=mytestlog.log
log4j.appender.stdout.MaxFileSize=100KB
log4j.appender.stdout.MaxBackupIndex=1

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=trace, stdout



LogClass.java

package com.zilun.utility;

import org.apache.log4j.Logger;

public class LogClass {
private static Logger log = Logger.getLogger(LogClass.class);

public static void writeLog() {
log.trace("Trace");
log.debug("Debug");
log.info("Info");
log.warn("Warn");
log.error("Error");
log.fatal("Fatal");
}
public static void main(String[] args) {
LogClass.writeLog();
}
}


callLog.jsp
From Drop Box


I found out that the application logs fine to console when run both from class main method and when called from jsp.

When i change the log4j.properties to use either RollingFileAppender or FileAppender it only works if run from class main method.Is there anything that i missed?

i only found out the answer after some times. 4 days to be exact (thats include 3 days hari raya holiday!!). Actually the only thing that i miss is the log file location. ehehe... tak perasan...

I am using eclipse and 2log4j project folder is in c:/eclipse/workspace/
The different is when i run the class main() method, the log file is created in c:/eclipse/workspace/2log4j/ directory but if i call the jsp file callLog.jsp the log file is created directly under eclipse root folder which is c:/eclipse
thats all..

The problem solved! :)

No comments: