tags:

views:

54

answers:

2

Hi, 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.

+1  A: 

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/ .

kaliatech
I did what you suggest but I keep seeing logs from com.bar in the console.
gulbrandr
I might have misunderstood what you were asking for originally. I revised my answer. Hope it helps.
kaliatech
Yes I want to log only com.foo to the console. I removed the console appender from the root but still, messages from com.bar appear in the console.
gulbrandr
Have you tried setting the log level for "com.bar" to "NONE" temporarily? If you do that and are still seeing messages, then you are not loading the log4j config that you think you are. I've done that a number of times. Otherwise, I'd suggest updating your question with your latest config XML.
kaliatech
A: 

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

Brad