create two log files using RollingFileAppender in log4j.xml

15,576

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.

Share:
15,576

Related videos on Youtube

Human Being
Author by

Human Being

Always willing to learn the new things to enhance my knowledge .

Updated on September 26, 2022

Comments

  • Human Being
    Human Being over 1 year

    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.

  • Human Being
    Human Being over 10 years
    Thanks for the quick reply.If I put the additivity="false" as <logger name="com.comp.logger1" additivity="false" > and <logger name="com.comp.logger2" additivity="false" > , My log data didn't logged in the log files
  • stripybadger
    stripybadger over 10 years
    Get rid of the additivity bits and also get rid of the root section - if you still see no logs then there's something wrong with the rest of the file. One difference (between your file and a file I have that I know works) is that my 'logger' sections have a level e.g. <level value="TRACE" />.
  • Human Being
    Human Being over 10 years
    Thanks for the reply.If I delete the root section means , My log file will not created in the folder.
  • Human Being
    Human Being over 10 years
    can you post your log4j.xml config and how you access the logger in JAVA class ?
  • stripybadger
    stripybadger over 10 years
    You're doing it right apart from a minor change, see edited answer.
  • Human Being
    Human Being over 10 years
    Yes yes yes yes. You are right.I got the solution as expected.Thanks thanks a lot.
  • Human Being
    Human Being over 10 years
    I will let you know if I have any doubts.
  • Human Being
    Human Being over 10 years
    Hi I have one doubt.if I use the Logger log = Logger.getLogger("any name"); and log as log.info("Test It"); . The log data was logged in both files. hoe to avoid when using the unknown logger name ?
  • stripybadger
    stripybadger over 10 years
    Your two loggers are com.comp.logger1 and com.comp.logger2. You haven't specified a logger for any name. Therefore, log4j will use the root logger. Because your root logger logs to both appenders, the log will show up in both files. If you don't want this to be the case, change your root logger.
  • Human Being
    Human Being over 10 years
    Thanks for the reply. But How to avoid the logging , when using the unknown logger name.Because I need this config.
  • Human Being
    Human Being over 10 years
    Please answer my question . I need this and its urgent.
  • stripybadger
    stripybadger over 10 years
    Did you read my comment!? "If you don't want this to be the case, change your root logger". Your root logger is telling log4j to log to both files when using "unknown logger name". If you don't want it to to do that, change your root logger. Or just remove it altogether.
  • Human Being
    Human Being over 10 years
    Yes you are right. I mis-understood your comment.Thanks my friend.