log4j将日志消息写入不同的文件

时间:2016-08-27 04:24:13

标签: java logging log4j

我在我的项目中使用log4j。我想要stdout,调试记录器和最终报告记录器。 Debug Logger几乎写了调试/信息级别消息。报告将是一个不同的记录器只写信息消息。我尝试了不同的方式,阅读了很多样本​​,我仍然无法弄清楚这一点。要么我丢失了标准输出,要么将所有调试写入两个记录器。

## Root logger option
log4j.rootLogger=DEBUG,stdout,debug_Log,reports_Log

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%F:%t:%L] - %m%n


log4j.appender.debug_Log=org.apache.log4j.RollingFileAppender
log4j.appender.debug_Log.File=c:\\debug.log
log4j.appender.debug_Log.MaxFileSize=10MB
log4j.appender.debug_Log.MaxBackupIndex=10
log4j.appender.debug_Log.maxFileSize=10MB 
log4j.appender.debug_Log.layout=org.apache.log4j.PatternLayout
log4j.appender.debug_Log.layout.ConversionPattern=%d-[%t-%F] %-5p %m%n

log4j.appender.reports_Log=org.apache.log4j.FileAppender
log4j.appender.reports_Log.File=c:\\Walmart.log
log4j.appender.reports_Log.layout=org.apache.log4j.PatternLayout
log4j.appender.reports_Log.layout.ConversionPattern=%d %-5p %m%n

log4j.logger.org.springframework=WARN  
log4j.logger.org.apache.component=WARN 
log4j.logger.org.apache.http=WARN
log4j.logger.org.apache.http.impl.conn=WARN
log4j.logger.org.apache.http.impl.client=WARN
log4j.logger.org.apache.http.client=WARN

log4j.logger.debug_Log=DEBUG, debug_Log
log4j.logger.reports_Log=INFO, reports_Log
log4j.logger.stdout=DEBUG, stdout
log4j.additivity.debug_Log=false
log4j.additivity.reports_Log=false
log4j.additivity.stdout=false

我的代码在这里

import org.apache.log4j.Logger;


static final Logger debugLog = Logger.getLogger("debug_Log");
static final Logger resultLog = Logger.getLogger("reports_Log");

public static void main(String[] args) {
    debugLog.info("Welcome to AmzCompareSale application");
    debugLog.debug("DEbugLog debug");

}

任何提示都将被赞赏=)

1 个答案:

答案 0 :(得分:0)

让我回答我自己的问题。这个post有助于描述logger和appender之间的区别。我一直在将我的代码链接到appender而不是创建logger并将appender附加到它上面。没有单独的appender和logger,我正在向两个.log文件打印消息。

这就是我最终要做的事情。

## Root logger option
log4j.rootLogger=DEBUG
#I want both debugAppender and reportsAppender to print to screen
log4j.logger.debug_Log=INFO, debugAppender, stdout
log4j.logger.reports_Log=INFO, reportsAppender, stdout


log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%F:%t:%L] - %m%n

log4j.appender.debugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.debugAppender.File=c:\\debug.log
log4j.appender.debugAppender.MaxFileSize=10MB
log4j.appender.debugAppender.MaxBackupIndex=10
log4j.appender.debugAppender.maxFileSize=10MB 
log4j.appender.debugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.debugAppender.layout.ConversionPattern=%d-[%t-%F] %-5p %m%n

log4j.appender.reportsAppender=org.apache.log4j.FileAppender
log4j.appender.reportsAppender.File=c:\\Walmart.log
log4j.appender.reportsAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.reportsAppender.layout.ConversionPattern=%d %-5p %m%n

#I just want to turn off spring rest template debug logging
log4j.logger.org.springframework=WARN  
log4j.logger.org.apache.component=WARN 
log4j.logger.org.apache.http=WARN
log4j.logger.org.apache.http.impl.conn=WARN
log4j.logger.org.apache.http.impl.client=WARN
log4j.logger.org.apache.http.client=WARN