Logback logger logging twice

后端 未结 3 471
忘掉有多难
忘掉有多难 2021-01-19 01:00

I would like to use slf4j+logback for two purposes in my application - log and audit.

For logging, I log the normal way:

static final Logger logger = L         


        
相关标签:
3条回答
  • 2021-01-19 01:36

    Change audit logger definition as shown below. Note additivity="false" flag in the logger definition.

    <logger name="AUDIT_LOGGER" level="info" additivity="false">
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{HH:mm:ss.SSS}|%msg%n
                </pattern>
            </encoder>
        </appender>
    </logger>
    

    That will avoid logging this again in root logger. Read logback configuration documentation for more information.

    0 讨论(0)
  • 2021-01-19 01:39

    I ran into the same problem, but I think the correct solution is different from what's proposed here through additivity. The attached diagram tries to visualize the two approaches that do not result in duplicate logs:

    1. For every <logger> you also add an <appender> and disable additivity. This is what's shown in the diagram under "Without additivity". The result is that each individual logger passes the logs directly to the appender and does not pass them to the ROOT logger.

    2. You specify the <logger>s and their log levels, but omit appender-refs for all but the ROOT logger, and leave additivty on. This is shown under "With addivity". The result is that a matching log from the com.foo logger is passed to the ROOT logger and uses the <appender> specified there. Note that, at that point, the log level filtering of the ROOT logger does not apply anymore. This means that even though com.foo is of log level INFO and the ROOT logger specifies ERROR, the log is still shown because it was already matched by com.foo.

    0 讨论(0)
  • 2021-01-19 01:57

    Solutions:

    1. Add additivity="false" to the "AUDIT_LOGGER", as described in one of the answers above, to not inherit the appenders from the root logger.

    2. Remove the appender element from the "AUDIT_LOGGER". This will cause the "AUDIT_LOGGER" to inherit the appenders from the root logger.

    3. Be careful including other config files, which can also cause duplicate logging. In my case, I had to remove base.xml include block, which defines a root logger and appenders.

    <include resource="org/springframework/boot/logging/logback/base.xml">
    

    NOTE: I would suggest defining the appenders outside of your loggers an referring to them by the appender-ref element. This way you can have multiple loggers referring to the same appender. See logback docs.

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
        <pattern>%d{HH:mm:ss.SSS}|%msg%n</pattern>
      </encoder>
    </appender>
    
    <logger name="AUDIT_LOGGER" level="info" additivity="false">
      <appender-ref ref="STDOUT" />
    </logger>
    
    
    
    0 讨论(0)
提交回复
热议问题