Log4J的配置

Log4J的配置
在web.xml文件中添加
     < servlet-name > log4j </ servlet-name >
    
< servlet-class > com.log4j.Log4jInit </ servlet-class >
    
< init-param >
    
< param-name > log4j </ param-name >
    
< param-value > /WEB-INF/log4j.properties </ param-value >
    
</ init-param >
    
< load-on-startup > 1 </ load-on-startup >
    
</ servlet >     
com.log4j.Log4jInit需要自己写,貌似新版log4J已经不带初始化文件了
import  org.apache.log4j. * ;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
public   class  Log4jInit  extends  HttpServlet  {
    
/** *//**
     * 
     
*/

    
private static final long serialVersionUID = 1L;
    
public void init() {
        String prefix 
=  getServletContext().getRealPath("/");
        String file 
= getInitParameter("log4j");
        
// if the log4j-init-file is not set, then no point in trying
        System.out.println(".log4j start");
        
if(file != null
        
{
            PropertyConfigurator.configure(prefix
+file);
            System.out.println(prefix
+file);
        }

    }

    
public void doGet(HttpServletRequest req, HttpServletResponse res) {
    }

}
普通应用程序中的Log4jInit
public   class  Log4jInit  {
    
private static final long serialVersionUID = 1L;
    
public static void main(String[] args) {
        PropertyConfigurator.configure(
"/src/log4j.properties");
    }


}


log4j.properties文件,放到WEB-INF目录下,具体每个参数是什么意思,查一下参考文档
log4j.rootLogger = debug ,  stdout ,  R

log4j.appender.stdout
= org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout
= org.apache.log4j.PatternLayout

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

log4j.appender.R
= org.apache.log4j.RollingFileAppender
log4j.appender.R.File
= example.log

log4j.appender.R.MaxFileSize
= 100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex
= 1

log4j.appender.R.layout
= org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern
= %p %t %c - %m%n

#log4j.appender.R 
=  org.apache.log4j.RollingFileAppender
#log4j.appender.R.File 
=  logs\\test.log
#log4j.appender.R.MaxFileSize 
=  1MB
#log4j.appender.R.MaxBackupIndex 
=   3
#log4j.appender.R.layout 
=  org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern 
= [ %d ]  %-4r  [ %t ]  %-5p %c %x - %m %n



---------------------------------------------------------
专注移动开发
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian

你可能感兴趣的:(Log4J的配置)