在log4j中,有没有办法让logger.error(“”)和logger.debug(“”)包含不同的输出布局?
编辑:
添加apache-log4j-extras后,以下配置文件有效.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" > <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="WARNINGS" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="warnings.log"/> <param name="DatePattern" value="'.'yyyy-MM-dd"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss} %-5p %c - %m%n%n"/> </layout> <filter class="org.apache.log4j.filter.LevelRangeFilter"> <param name="LevelMin" value="DEBUG"/> <param name="LevelMax" value="WARN"/> </filter> </appender> <appender name="ERRORS" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="errors.log"/> <param name="DatePattern" value="'.'yyyy-MM-dd"/> <layout class="org.apache.log4j.PatternLayout"> <!-- %M for method name,%L for line number --> <param name="ConversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss} %-5p %c %M %L - %m%n%n"/> </layout> <filter class="org.apache.log4j.filter.LevelRangeFilter"> <param name="LevelMin" value="ERROR"/> <param name="LevelMax" value="FATAL"/> </filter> </appender> <logger name="com.foo.bar"> <appender-ref ref="WARNINGS"/> <appender-ref ref="ERRORS"/> </logger> </log4j:configuration>
这段代码用于测试
// goes to errors.txt log.error("error"); //goes to warnings.txt log.warn("warn");
解决方法
在log4j.xml配置中,您可以创建两个appender,每个appender使用一个
LevelMatchFilter,每个appender都有一个单独的模式输出.