1) I am using log4j-1.2.8.jar
2) Import this jar file into your WEB_INF\lib directory
3) I have writter a wrapper class for Log4J and I have placed it in package log under
Java Source
I am pasting some sample code here.
**********************************************
Java Source\log\Logger.java
**********************************************
package log;
import org.apache.log4j.*;
import java.util.*;
import java.io.*;
import java.net.*;
/**
* This is a wrapper class for Log4J.
*/
public class Logger {
Category cat;
// Logger class name
static
String Nm = Logger.class.getName();
/** Constructor which returns the root content
*/
public Logger() {
cat = Category.getRoot();
}
/** creates an instance of the desired class name
* @param name of class
*/
public Logger(Class name)
{
cat = Category.getInstance(name.getName());
}
/** Configuration settings
* @param propsFile full path name of properties file
* @param refresh in milliseconds. This reads the properties file at the specified intervals.
*/
public static void configureAndWatch(String propsFile, int refresh) {
PropertyConfigurator.configureAndWatch(propsFile,refresh);
}
/** log a debug message
* @param msg
*/
public void debug(String msg)
{
cat.log(Nm,Priority.DEBUG,msg,null);
}
/** log a debug message
* @param msg
* @param t a Throwable
*/
public void debug(String msg,Throwable t)
{
cat.log(Nm,Priority.DEBUG,msg,null);
}
// do similar for info,fatal,error and warn
}
************************************************
5) It should compile without any errors.
6) I have got this settings file in my local C:\. You can place it anywhere you want.
***********settings.prop*****************
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
#log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#log4j.appender.A1.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.A1.layout.ConversionPattern=%d [%-5p] (%F:%L) - %m\r\n
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=C:\\mylog.log
*****************************************
7) I am now using this Logger from my
JSP Page which is under Web Content
**********LogTest.jsp***************
log.Logger logging =new log.Logger(log.Logger.class);
logging.configureAndWatch("C:\\settings.prop",3);
logging.debug("test test test message");
************************************
Good luck !!!
Originally posted by xin wen:
Hi,
I still cannot make it work. I deleted the log file, when I ran the project this time, the log file was not generated at all, do not know why. I wander if the common-loggins.jar which is the log jar WSAD itself causes this to be happen, so is that the reason it does not load my .properties file properly? Anyone had this problem before?
Thanks