Why is Log4j rootLogger not filtering log events according to event level?

后端 未结 3 1927
一生所求
一生所求 2021-01-02 15:50

Why is the Log4j rootLogger in my application not filtering log events according to level? In my log4j.properties, I have several loggers:

3条回答
  •  天涯浪人
    2021-01-02 16:15

    See this answer to a similar question about logger chaining in Log4j:

    The way Log4j chaining works is a bit counter intuitive (to me at least). If the request level is equal to or above the threshold of the most specific matching logger, it is accepted. Once the request is accepted, it gets handled by the complete chain of ancestors regardless of their thresholds!

    This means that no matter to what level you set the threshold of the root logger, it will always accept and output the log event that any other logger accepts, unless you disable chaining for that child logger or explicitly set the threshold of its appender to a higher level.

    So, in this case, there are two ways that you can prevent root logger from capturing the events from the other loggers. The first is the more selective approach of disabling log event chaining:

    log4j.additivity.com.name.myapp=false
    log4j.additivity.org.castor=false
    log4j.additivity.org.exolab.castor=false
    log4j.additivity.org.hibernate=false
    log4j.additivity.org.springframework=false
    

    The second way is simpler, but more restrictive since it suppresses all events on the console that are lower than INFO (DEBUG and TRACE):

    log4j.appender.stdout.Threshold=info
    

提交回复
热议问题