开发者

Problem with log4j xml configuration

i have little question, my log4j.xml configuration is listed below

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
        </layout>
    </appender>
     <logger name="com.alsodeep">
        <level value="info" />
        <appender-ref ref="console" />
    </logger>  
    &开发者_JAVA百科lt;logger name="org.springframework">
        <level value="error" />
        <appender-ref ref="console" />
    </logger>
    <root>
        <priority value="error" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>

and when i call a log.info("some info") inside the com.alsodeep package it printed out twice, for example

INFO  AspectExample - Call it before method
INFO  AspectExample - Call it before method
Hello
INFO  AspectExample - Call it after method
INFO  AspectExample - Call it after method
List content [[23, 243, 234, 23432]]
INFO  App - final
INFO  App - final

i'm confused with this, whats wrong with my log4j settings.

PS when i remove appender from root element, everything is ok and my log info appear only once.


Remove:

<appender-ref ref="console" />

from particular loggers and leave only one in root logger:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
        </layout>
    </appender>
     <logger name="com.alsodeep">
        <level value="info" />
    </logger>  
    <logger name="org.springframework">
        <level value="error" />
    </logger>
    <root>
        <priority value="error" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>

Your configuration is valid, however when you try to log something on com.alsodeep, it first gets appender to the appender associated directly to this logger and then propagates up. com has no appender associated, but root (parent of all loggers) has. By coincidence it is the same console appender, so it gets appended twice.


Another possible answer: (assuming that your example was simplified from a more complex logger/appender scheme)

You can set the additivity (see the "Appenders and Layout" section) attribute on each logger to false. This will prevent the log event from propagating up the logger hierarchy.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜