Log4j如何按级别进行日志记录,日志文件应仅包含其级别的日志

时间:2015-01-30 08:24:31

标签: java logging log4j

我希望我的日志文件只显示他们的日志级别信息。我的意思是info.log应该只显示信息日志而不是错误或警告或调试日志。我可以通过为每个日志级别实例化对象来实现这一点。我的意思是我对所有级别都有_traceLogger,_infoLogger,_warnLogger等。这不是我想要的。这是我的log4j.properties文件。

log4j.rootLogger=TRACE,TraceFileAppender,DebugFileAppender,InfoFileAppender,WarnFileAppender,ErrorFileAppender,FatalFileAppender

# TraceFileAppender - used to log messages in the trace.log file.
log4j.appender.TraceFileAppender=org.apache.log4j.FileAppender
log4j.appender.TraceFileAppender.File=./logs/trace.log
log4j.appender.TraceFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.TraceFileAppender.layout.ConversionPattern=[%-5p]%d{yyyy-MM-dd HH:mm:ss} - %m%n

# DebugFileAppender - used to log messages in the debug.log file.
log4j.appender.DebugFileAppender=org.apache.log4j.FileAppender
log4j.appender.DebugFileAppender.File=./logs/debug.log
log4j.appender.DebugFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugFileAppender.layout.ConversionPattern=[%-5p]%d{yyyy-MM-dd HH:mm:ss} - %m%n

# InfoFileAppender - used to log messages in the info.log file.
log4j.appender.InfoFileAppender=org.apache.log4j.FileAppender
log4j.appender.InfoFileAppender.File=./logs/info.log
log4j.appender.InfoFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.InfoFileAppender.layout.ConversionPattern=[%-5p]%d{yyyy-MM-dd HH:mm:ss} - %m%n

# WarnFileAppender - used to log messages in the warn.log file.
log4j.appender.WarnFileAppender=org.apache.log4j.FileAppender
log4j.appender.WarnFileAppender.File=./logs/warn.log
log4j.appender.WarnFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.WarnFileAppender.layout.ConversionPattern=[%-5p]%d{yyyy-MM-dd HH:mm:ss} - %m%n

# ErrorFileAppender - used to log messages in the error.log file.
log4j.appender.ErrorFileAppender=org.apache.log4j.FileAppender
log4j.appender.ErrorFileAppender.File=./logs/error.log
log4j.appender.ErrorFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorFileAppender.layout.ConversionPattern=[%-5p]%d{yyyy-MM-dd HH:mm:ss} - %m%n

# FatalFileAppender - used to log messages in the fatal.log file.
log4j.appender.FatalFileAppender=org.apache.log4j.FileAppender
log4j.appender.FatalFileAppender.File=./logs/fatal.log
log4j.appender.FatalFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.FatalFileAppender.layout.ConversionPattern=[%-5p]%d{yyyy-MM-dd HH:mm:ss} - %m%n


log4j.logger.trace=TRACE,TraceFileAppender
log4j.logger.debug=DEBUG,DebugFileAppender
log4j.logger.info=INFO,InfoFileAppender
log4j.logger.warn=WARN,WarnFileAppender
log4j.logger.error=ERROR,ErrorFileAppender
log4j.logger.fatal=FATAL,FatalFileAppender

有没有办法为所有人只实例化一个记录器对象?

这是java代码:

   PropertyConfigurator.configure("log4j.properties");
        _traceLogger = org.apache.log4j.Logger.getLogger("trace");
        _debugLogger = org.apache.log4j.Logger.getLogger("debug");
        _infoLogger = org.apache.log4j.Logger.getLogger("info");
        _warnLogger = org.apache.log4j.Logger.getLogger("warn");
        _errorLogger = org.apache.log4j.Logger.getLogger("error");
        _fatalLogger = org.apache.log4j.Logger.getLogger("fatal");

使用此代码,它按预期工作。但我需要一个记录器对象。

*编辑

我想,我需要像上界这样的东西。我的意思是有阈值,每个appender也需要上限。如果我错了,请纠正我。

正如评论中指出的那样。这个问题在这里已有答案:

can we have level based log files using log4j
log different log levels to different files with log4j

0 个答案:

没有答案