1

My Java project contains following packages - com.main.log4j.main , com.main.log4j.other.

I want log lines of package "com.main.log4j.other" to be removed from Console log, rather maintain a different log file for the same package. I am using Log4j version 1.2.16, with following log4j.xml config.

<appender name="CONSOLE-LOG" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="..." />
    </layout>
</appender>

<appender name="OTHER-LOG" class="org.apache.log4j.FileAppender">
    <param name="File" value="logs/Others.log" />
    <param name="Threshold" value="DEBUG" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="..." />
    </layout>
</appender>

<category name="com.main.log4j.other">
    <appender-ref ref="OTHER-LOG" />
</category>
<root>
    <level value="debug" />
    <appender-ref ref="CONSOLE-LOG" />
</root>

Using this config, a separate log file Others.log is created with log line, but those log lines are still present in console log. I want to exclude them. Please suggest me how I can configure the log4j.xml?

1 Answers1

0

Set the additivity to false in your "com.main.log4j.other" category :

<category name="com.main.log4j.other" additivity="false">

This will prevent those logs to be also logged in the root logger.

Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • Posted a new Problem statement - How to resolve Warning : Unrecognized element - rollingPolicy , triggeringPolicy? https://stackoverflow.com/questions/54805122/log4j-how-to-resolve-warning-unrecognized-element-rollingpolicy-triggeri – Biswajit Sarkar Feb 21 '19 at 10:43
  • Can we achieve the following ? 1. I want FATAL level logs of package "com.main.log4j.other" will remain the part of Console log. 2. Only DEBUG & INFO level Log lines of package "com.main.log4j.other" will be part of "Others.log". Is there any way to do both of this? NB: Part 2 - can be achieved by using LevelMatchFilter and DenyAllFilter. – Biswajit Sarkar Feb 21 '19 at 15:36