我希望log4j输出INFO级别到info.log和DEBUG级别到debug.log,以便使用这段代码:
logger.info("This is an info message");
logger.debug("This is a debug message");
只有第一个会转到info.log,而两个都转到debug.log。
这不起作用:
log4j.rootLogger=INFO, R, T
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%r %-5p %c %x - %m%n
log4j.appender.R=org.apache.log4j.FileAppender
log4j.appender.R.File=info.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.R.Threshold = INFO
log4j.appender.T=org.apache.log4j.FileAppender
log4j.appender.T.File=debug.log
log4j.appender.T.layout=org.apache.log4j.PatternLayout
log4j.appender.T.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.T.Threshold = DEBUG
这两个都没有:
log4j.rootLogger=INFO, R
log4j.rootLogger=DEBUG, T
log4j.appender.R=org.apache.log4j.FileAppender
log4j.appender.R.File=info.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.R.Threshold = INFO
log4j.appender.T=org.apache.log4j.FileAppender
log4j.appender.T.File=debug.log
log4j.appender.T.layout=org.apache.log4j.PatternLayout
log4j.appender.T.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.T.Threshold = DEBUG
答案 0 :(得分:0)
您可以使用过滤器org.apache.log4j.varia.LevelMatchFilter
。 e.g:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN"
"log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- INFO appender -->
<appender name="INFOFILE" class="org.apache.log4j.FileAppender">
<param name="file" value="info.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern"
value="%d{yyyyMMdd-HHmmss.SSS} %-5p (%c.java:%L).%M - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="levelToMatch" value="INFO" />
</filter>
</appender>
<!-- DEBUG appender -->
<appender name="DEBUGFILE" class="org.apache.log4j.FileAppender">
<param name="file" value="debug.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern"
value="%d{yyyyMMdd-HHmmss.SSS} %-5p (%c.java:%L).%M - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="levelToMatch" value="DEBUG" />
</filter>
</appender>
<!-- root -->
<root>
<priority value="ALL" />
<appender-ref ref="INFOFILE" />
<appender-ref ref="DEBUGFILE" />
</root>
</log4j:configuration>
将所有这些放在默认包中的一个文件中:log4j.xml
。