`

log4j配置日志

 
阅读更多

1.首先下载log4j.jar包

2.导入到项目中,

3.建一个配置文件,log4j.properties
配置参考log4j配置文档

4.测试
配置如下:
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=fatal,rollingfile,stdout

### direct log messages to console ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=error
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %l %x - %m%n

### direct messages to file hibernate.log ###
log4j.appender.rollingfile=org.apache.log4j.RollingFileAppender
#log4j.appender.rollingfile.Threshold=debug
log4j.appender.rollingfile.File=c:/logs/test.log
log4j.appender.rollingfile.Append=true
log4j.appender.rollingfile.MaxFileSize=20MB
log4j.appender.rollingfile.MaxBackupIndex=25
log4j.appender.rollingfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingfile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %l %x - %m%n


代码如下:
package test;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

//import log4j.Logger;

public class TestLog4j {

/**
* @param args
*/
public void log4j(){
Logger logger=Logger.getLogger(this.getClass().getName());
System.out.println("test log4");
logger.fatal("this is a log4j test!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//PropertyConfigurator.configure("C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/metera/WEB-INF/classes/log4j.properties");
TestLog4j testLog4j=new TestLog4j();
testLog4j.log4j();

}

}


5.运行后
可以看到生成了一个test.log文档。表示成功了,那可以继续添加到其它需要的板块了

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics