Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use different log4j conversion patterns for different packages in any application

Tags:

java

log4j

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>
like image 709
Sameer Avatar asked Sep 10 '25 08:09

Sameer


2 Answers

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

logger level

For more details read this

like image 122
deepakmodak Avatar answered Sep 12 '25 20:09

deepakmodak


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.

like image 22
Heiko Schmitz Avatar answered Sep 12 '25 22:09

Heiko Schmitz