Sumit
Sumit

Reputation: 39

Log4j multiple log files using rootLogger

I want to store all log details of my entire application in two different files. For eg: one file will contain all DEBUG information and the other will contain only ERROR

my current log4j.properties file looks like this

log4j.rootLogger=DEBUG, file1
#Log Message in debug.log file
log4j.appender.file1=org.apache.log4j.FileAppender
log4j.appender.file1.File=log/debug.log
log4j.appender.file1.layout=org.apache.log4j.PatternLayout
log4j.appender.file1.layout.ConversionPattern=%-4r %d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n

log4j.rootLogger=ERROR, file2
#Log Message in error.log file
log4j.appender.file2=org.apache.log4j.FileAppender
log4j.appender.file2.File=log/error.log
log4j.appender.file2.layout=org.apache.log4j.PatternLayout
log4j.appender.file2.layout.ConversionPattern=%-4r %d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n

but this only updates the error.log file(any file mentioned last)

The best solution that i have come across till now is using

log4j.logger.OTHER_LOGGER=DEBUG, file2
log4j.additivity.OTHER_LOGGER = false
#File Appender
log4j.appender.file2=org.apache.log4j.FileAppender
log4j.appender.file2.File=log/admin.log
log4j.appender.file2.layout=org.apache.log4j.PatternLayout
log4j.appender.file2.append=false
log4j.appender.file2.layout.ConversionPattern=%-4r %d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n

and using

private static final Logger logger = LoggerFactory.getLogger("OTHER_LOGGER");

but the problem in using this is when the log is updated is shows like

0    22 Oct 2012 20:38:50 [main] INFO  OTHER_LOGGER  - testclass

Now here instead of the mentioning the class from where the log is reported, it says OTHER_LOGGER

Thus the best way to go about it would be using two rootLooger's but somehow it does not allow it

Could anyone please help me out?

Upvotes: 2

Views: 7016

Answers (1)

Artem Shafranov
Artem Shafranov

Reputation: 2673

Again, it's the good job for LevelMatchFilter. Just use LevelMatchFilter with property LevelToMatch = ERROR for the appender "file2".

You should use XML configuration because filters are unsupported by properties configuration. Here I tried to write XML configuration that you need:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="file1" class="org.apache.log4j.FileAppender"> 
            <param name="File" value="log/debug.log"/> 
            <layout class="org.apache.log4j.PatternLayout"> 
                    <param name="ConversionPattern" value="%-4r %d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n"/> 
            </layout> 
    </appender> 

    <appender name="file2" class="org.apache.log4j.FileAppender"> 
            <param name="File" value="log/error.log"/> 
            <layout class="org.apache.log4j.PatternLayout"> 
                    <param name="ConversionPattern" value="%-4r %d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n"/> 
            </layout> 

            <!-- Use LevelMatchFilter to write only ERROR messages to error.log -->
            <filter class="org.apache.log4j.varia.LevelMatchFilter">
                    <param name="LevelToMatch" value="error" />
                    <param name="AcceptOnMatch" value="true"/>
            </filter>
            <filter class="org.apache.log4j.varia.DenyAllFilter" />
    </appender> 

    <root> 
            <level value="debug"/> 
            <appender-ref ref="file1" /> 
            <appender-ref ref="file2" /> 
    </root> 
</log4j:configuration>

Upvotes: 1

Related Questions