我想要有多个log4j appender,一个用于文件中的消息级别,以及所有打印在控制台中。但我没有。
我的实际log4j.properties:
log4j.rootLogger=ALL, CONSOLA
log4j.appender.CONSOLA=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLA.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLA.layout.ConversionPattern=%-4r %d %-5p %c - %m%n
log4j.appender.WARNLOG=org.apache.log4j.FileAppender
log4j.appender.WARNLOG.threshold=WARN
log4j.appender.WARNLOG.file=XX/web/WEB-INF/serverlogs/warn-server.log
log4j.appender.WARNLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.WARNLOG.layout.ConversionPattern=%-4r %d %-5p %c - %m%n
log4j.appender.WARNLOG.append=true
log4j.appender.WARNLOG.MaxFileSize=250KB
log4j.appender.WARNLOG.MaxBackupIndex=backup
log4j.appender.FATALLOG=org.apache.log4j.FileAppender
log4j.appender.FATALLOG.threshold=FATAL
log4j.appender.FATALLOG.file=XX/web/WEB-INF/serverlogs/fatal-server.log
log4j.appender.FATALLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.FATALLOG.layout.ConversionPattern=%-4r %d %-5p %c - %m%n
log4j.appender.FATALLOG.append=true
log4j.appender.FATALLOG.MaxFileSize=250KB
log4j.appender.FATALLOG.MaxBackupIndex=backup
log4j.logger.com.XX=WARN, WARNLOG
log4j.logger.com.XX=FATAL, FATALLOG
解决:
我使用 LevelRangeFilter
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-4r %d %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="FATALLOG" class="org.apache.log4j.FileAppender">
<param name="File" value="xx/web/WEB-INF/serverlogs/fatal-server.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-4r %d %-5p %c - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="FATAL"/>
<param name="LevelMin" value="FATAL"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
</appender>
<appender name="WARNLOG" class="org.apache.log4j.FileAppender">
<param name="File" value="xx/web/WEB-INF/serverlogs/warn-server.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-4r %d %-5p %c - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="WARN"/>
<param name="LevelMin" value="WARN"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
</appender>
<logger name="com.xx">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FATALLOG" />
<appender-ref ref="WARNLOG" />
</logger>
</log4j:configuration>