Java – different log4j layouts for debugging and errors?
•
Java
In log4j, is there any way to make logger Error ("") and logger Debug ("") contains different output layouts?
I think errors include method names and line numbers, both of which will reduce application performance
Edit:
After adding Apache log4j extras, the following configuration files are valid
<?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>
This code is used for testing
// goes to errors.txt log.error("error"); //goes to warnings.txt log.warn("warn");
Solution
In log4j In XML configuration, you can create two Appenders. Each appender uses a levelmatchfilter, and each appender has a separate schema output
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码