how to use different log4j conversion pattern for logging messages in different packages and use same file for output.
Below is my configuration file, please suggest how to modify in order to use different conversion patterns
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="PLUGIN_FILE" class="LoggerTest.NewLogForEachRunFileAppender">
<!-- Below param sets the dir path of the log files -->
<param name="FileDirPath" value ="/var/opt/mycomp/ftpm/" />
<!-- Below param sets the suffix name for the log file -->
<param name="FileNameSuffix" value="_error.log" />
<param name="MaxFileSize" value="10KB"/>
<!-- Below param creates the specified number of backup files to be created when rolled back -->
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-4r %d [%t] %-5.37c %M() %L %x %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="fatal" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="ERROR_FILE" class="LoggerTest.NewLogForEachRunFileAppender">
<!-- Below param sets the dir path of the log files -->
<param name="FileDirPath" value ="/var/opt/mycomp/ftpm/" />
<!-- Below param sets the suffix name for the log file -->
<param name="FileNameSuffix" value="_error.log"/>
<param name="MaxFileSize" value="10KB"/>
<!-- Below param creates the specified number of backup files to be created when rolled back -->
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="error" />
<param name="LevelMax" value="fatal" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<logger name="LoggerTest.a" additivity="false">
<appender-ref ref="PLUGIN_FILE"/>
</logger>
<root>
<level value="error" />
<appender-ref ref="ERROR_FILE" />
<!-- To enable the trace messages for debugging uncomment the below appender ref statement -->
<level value="DEBUG" />
<appender-ref ref="DEBUG_FILE" />
</root>
</log4j:configuration>
On going through your log4j config file, I can figure out that you want to capture all logs with level DEBUG
in one file and all logs with level ERROR
in another file with different conversion pattern for each file.
If it so then you have to device some other mechanism to implement it because when you define level value in root logger then it will capture all logs with level mentioned along with all below than that in logging hierarchy
OFF
FATAL
ERROR
WARN
INFO
DEBUG
TRACE
ALL
For more details read this
I think that it is not possible, since the conversion pattern is assigned to the FileAppender. Do you have a good reason why you want to put different patterns into the same file? It will make reading/parsing the log difficult.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With