我怎样才能创build2个单独的日志文件与一个log4jconfiguration文件?
我无法弄清楚如何configuration我的log4j,以便我的debugging日志和我的reportsLog是彼此分开(不添加)。 为什么在下面的configuration中,reportsLog总是空的?
log4j.rootLogger=TRACE, stdout, debugLog log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n log4j.appender.debugLog=org.apache.log4j.FileAppender log4j.appender.debugLog.File=logs/debug.log log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n log4j.reportsLog=DEBUG,reportsLog log4j.appender.reportsLog=org.apache.log4j.FileAppender log4j.appender.reportsLog.File=logs/reports.log log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
这是我的Java代码:
package test; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class HelloLogger { static final Logger debugLog = Logger.getLogger("debugLog"); static final Logger resultLog = Logger.getLogger("reportsLog"); public static void main(String[] args) { PropertyConfigurator.configure("log4j.properties"); debugLog.debug("Hello debugLog message"); resultLog.debug("Hello reportsLog message"); } }
尝试以下configuration:
log4j.rootLogger=TRACE, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n log4j.appender.debugLog=org.apache.log4j.FileAppender log4j.appender.debugLog.File=logs/debug.log log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n log4j.appender.reportsLog=org.apache.log4j.FileAppender log4j.appender.reportsLog.File=logs/reports.log log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n log4j.category.debugLogger=TRACE, debugLog log4j.additivity.debugLogger=false log4j.category.reportsLogger=DEBUG, reportsLog log4j.additivity.reportsLogger=false
然后相应地在Java代码中configurationlogging器:
static final Logger debugLog = Logger.getLogger("debugLogger"); static final Logger resultLog = Logger.getLogger("reportsLogger");
你想要输出到stdout
吗? 如果不是,请将log4j.properties
的第一行更改为:
log4j.rootLogger=OFF
并摆脱stdout
线。
相应地修改您的log4j.properties
文件:
log4j.rootLogger=TRACE,stdout ... log4j.logger.debugLog=TRACE,debugLog log4j.logger.reportsLog=DEBUG,reportsLog
根据您的需要更改每个logging器的日志级别。
稍微修改Laz的configuration,使其工作(在第7):
log4j.category.infoLog=INFO, infoLog log4j.additivity.infoLog=false log4j.category.debugLog=DEBUG, debugLog log4j.additivity.debugLog=false