create two log files using RollingFileAppender in log4j.xml

16.4k views Asked by At

My log4j.xml configuration was like ,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >

<log4j:configuration>


    <appender name="fileAppender1" class="org.apache.log4j.RollingFileAppender">

        <param name="Threshold" value="ALL" />
        <param name="MaxFileSize" value="3KB" />
        <param name="MaxBackupIndex" value="10" />
        <param name="File" value="F:/logs/Testing/Project_moduleOne.log" />

        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
        </layout>
    </appender>

    <appender name="fileAppender2" class="org.apache.log4j.RollingFileAppender">

        <param name="Threshold" value="ALL" />
        <param name="MaxFileSize" value="3KB" />
        <param name="MaxBackupIndex" value="10" />
        <param name="File" value="F:/logs/PAD_Testing/Project_moduleTwo.log" />


        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
        </layout>
    </appender>


    <!--sets the priority log level for org.springframework -->
    <logger name="com.comp.logger1">
         <appender ref="fileAppender1"/>
    </logger>


    <logger name="com.comp.logger2">
        <appender ref="fileAppender2" />
    </logger>


    <!--sets the default priority log level -->
    <root>
        <priority value="all"></priority>
        <appender-ref ref="fileAppender1" />
        <appender-ref ref="fileAppender2" />
    </root>

</log4j:configuration>

And two log files also created in the specified location.

I need to know how to log two different data's in these two different log_files independently in JAVA class.

For example,

Logger logOne = Logger.getLogger("com.comp.logger1");
Logger logTwo = Logger.getLogger("com.comp.logger2");

The above code is not working for me. All the log informations are logged to both the created two log files. I need the separation of logging data.

My need is ,

  1. I want to create two log file. Because my project has two modules and log each module in separate log files.

  2. After that , I have to log each module logging data independently .

  3. Please make sure I used the logger name for logging in my java class correctly.

Any new or complete examples using log4j.xml are greatly appreciated.

EDIT :

If I add the additivity="false" in the logger as,

<logger name="com.comp.logger1" additivity="false">
     <appender ref="fileAppender1" /> 
</logger>


<logger name="com.comp.logger2" additivity="false">
    <appender ref="fileAppender2" />
</logger>

The log data didn't logged in the created log file.The log file was empty.

Please make sure my <root>...</root> is correct.

1

There are 1 answers

13
stripybadger On BEST ANSWER

You problem is with the <root> section, the root section captures all logs, and you've told it to log to both appenders...

You could remove it, or set additivity="false" on each of your logger elements - this will tell log4j not to log the same log through 'root' if it's already been logged through one of your 'logger's.

Edit: you don't say which version of log4j you are using, but I'm using log4j-1.2.16, and using the file in your post, it fails completely for me because it doesn't like the <appender ref="fileAppender1"/>, it wants them to be <appender-ref ref="fileAppender1"/> (note the -ref after the appender). If I add these in, and also add the additivity attributes that I suggested, then for me it works as you expect.