此文档放在项目编译文件.class相同的位置
### set log levels ###log4j.rootLogger = debug , stdout , D### 输出到控制台 ###log4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n### 输出到日志文件 ###log4j.appender.D = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.D.File = logs/log.loglog4j.appender.D.Append = truelog4j.appender.D.Threshold = DEBUGlog4j.appender.D.layout = org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n### 保存异常信息到单独文件 ###log4j.appender.D = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.D.File = logs/error.loglog4j.appender.D.Append = truelog4j.appender.D.Threshold = ERRORlog4j.appender.D.layout = org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
配置获取方法:
PropertyConfigurator.configure( "D:\\Project Files\\myeclipse\\commontest\\bin\\log\\log4j\\log4j.properties" );
Logger log=Logger. getLogger(TestLog4j.class);
log.warn( "嘿嘿");
log.debug( "呜呜");
log.error( "错啦");
在log4j的配置文件里面配置。log4j.appender.stdout.File=路径