开发者

log4j: how to log only messages from com.foo.* in the console?

开发者 https://www.devze.com 2023-01-22 06:21 出处:网络
in the system console I would like to log only messages from com.foo.* and not messages from 开发者_开发问答an external library that I\'m using com.bar.*

in the system console I would like to log only messages from com.foo.* and not messages from 开发者_开发问答an external library that I'm using com.bar.*

Here's what I did:

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %p %c - %m%n" />
    </layout>
</appender>

<appender name="bar" class="org.apache.log4j.FileAppender">
    <param name="File" value="logs/bar.log"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %p %c - %m%n" />
    </layout>
</appender>

<logger name="com.foo">
    <level value="ALL" />
    <appender-ref ref="console"/>
</logger>

<logger name="com.bar">
    <level value="ALL" />
    <appender-ref ref="bar"/>
</logger>

<root>
    <level value="ALL" />
    <appender-ref ref="console" />
            <appender-ref ref="bar" />
</root>

The problem is that log messages from com.bar.* are still displayed in the console.

Thanks.


If you want to log only com.foo to the console, then I think you can just remove the console appender reference from the root to get what you want.

If instead you want to prevent only com.bar from logging to the console, then I think this would work:

<logger name="com.foo">
    <level value="ALL" />
</logger>

<logger name="com.bar" additivity="false">
    <level value="ALL" />
    <appender-ref ref="bar"/>
</logger>

Keep in mind that loggers have additivity set to true by default. See: http://veerasundar.com/blog/2009/08/log4j-tutorial-additivity-what-and-why/ .


You can always set the level for com.bar.* to error

0

精彩评论

暂无评论...
验证码 换一张
取 消