log4j writing downsteam's logs to a file

2019-08-21 07:53发布

问题:

I have a java application. I have created a log4j2.xml under src folder and that works as expected.

This java application depends on another java package. I tried creating a similar log4j2.xml in this package to format all the logging done in package, But this does not seem to work. And all the logs in the package seems to be going to console in default formatting.

Thoughts?

回答1:

In this case you need to have different Appenders

SO the config will be as below

<!-- Package 1 appender declaration -->
<appender name="one" class="org.apache.log4j.FileAppender">
    ...
</appender> 

<!-- Package 2 appender declaration -->
<appender name="two" class="org.apache.log4j.FileAppender">
    ...
</appender>

<!-- logger package com.myapp.app.one -->
<logger name="com.myapp.app.one">
    <level value="DEBUG" />
    <appender-ref ref="one"/>
</logger>

<!-- logger for package com.myapp.app.two -->
<logger name="com.myapp.app.two">
    <level value="DEBUG" />
    <appender-ref ref="two"/>
</logger>


标签: log4j2