In our previous post, we learned how to add a variable to the log file name. This time, we are going to separate the logs based on their levels so that a log file has only one log level (or more if you want).
In a typical log4j properties file, you can define a threshold, which allows you to record logs whose levels are in the defined level or above.
For example,
FATAL: shows messages at a FATAL level only
ERROR: Shows messages classified as ERROR and FATAL
INFO: Shows messages classified as INFO, WARNING, ERROR, and FATAL etc...
In order to filter out a range of levels to record, we can use Apache's LevelRangeFilter in which we can define LevelMin and LevelMax parameters to filter the logs to record. As of now, I could find only the xml implementation of this feature.
So if you want a file to have only WARN level messages, all you got to do is to add the following entry in your log4j.xml inside the <appender>.
A complete log4j.xml file that has defined a file to log warnings and errors will look like the following.
Cheers!
For example,
FATAL: shows messages at a FATAL level only
ERROR: Shows messages classified as ERROR and FATAL
INFO: Shows messages classified as INFO, WARNING, ERROR, and FATAL etc...
In order to filter out a range of levels to record, we can use Apache's LevelRangeFilter in which we can define LevelMin and LevelMax parameters to filter the logs to record. As of now, I could find only the xml implementation of this feature.
So if you want a file to have only WARN level messages, all you got to do is to add the following entry in your log4j.xml inside the <appender>.
<filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="WARN"/> <param name="LevelMax" value="WARN"/> </filter>
A complete log4j.xml file that has defined a file to log warnings and errors will look like the following.
<?xml version="1.0" encoding="UTF-8" ?> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false"> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/> </layout> </appender> <!--Warn logs to be printed--> <appender name="warn-file" class="org.apache.log4j.RollingFileAppender"> <param name="append" value="false"/> <param name="maxFileSize" value="10MB"/> <param name="maxBackupIndex" value="10"/> <param name="file" value="warn_log.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="WARN"/> <param name="LevelMax" value="WARN"/> </filter> </appender> <!--Error logs to be printed--> <appender name="error-file" class="org.apache.log4j.RollingFileAppender"> <param name="append" value="false"/> <param name="maxFileSize" value="10MB"/> <param name="maxBackupIndex" value="10"/> <param name="file" value="error_log.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="ERROR"/> <param name="LevelMax" value="ERROR"/> </filter> </appender> <root> <level value="INFO"/> <appender-ref ref="console"/> <appender-ref ref="warn-file"/> <appender-ref ref="error-file"/> </root> </log4j:configuration>
Cheers!