Search code examples
javaxmlapachelog4jappender

How to move logs from console to a file?


In our project, currently we have a main portal-log4j.xml and in our project we need to override it using portal-log4j-ext.xml In the portal-log4j.xml, we have all logging being done to the console by default.

    <root>
    <priority value="INFO" />
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="FILE" />
</root>

In the overridden portal-log4j-ext.xml file, we have the following code :

<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
    <param name="maxFileSize" value="10000kb" />
    <param name="maxBackupIndex" value="20" />
    <param name="file" value="D:/myLogFile.log" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="&#37;d{ISO8601} &#37;-5p [&#37;C{1}] &#37;x: &#37;m&#37;n"/>
    </layout>
</appender>     

<logger name="com.company.layoutimporter">
    <level value="DEBUG" />
    <appender-ref ref="FILE" />
</logger>

As you can see, I am able to write to a file, but its logging in the console as well. How can we override this default behaviour so that I can log my package specific messages only to the particular log file (and not to console).

Thanks in advance !


Solution

  • found a solution myself.. had to just add additivity="false" to the logger tag.

    This will basically skip the logging again by the parent tags.

    More info : Additivity - What and Why ?