Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid double logging with logback? [duplicate]

In my application, I would like to log some messages coming from my own code in a specific manner compared to all other messages being logged. However I am not sure how can I avoid them also being automatically logged to the logack root logger.

Using this configuration below, I would like to use code like follows (scala) so that I can log certain messages only to that logger.

val logger: Logger = LoggerFactory.getLogger("data-logger")

However in the configuration below, these messages get logged twice, i.e. they are logged also by the root logger. How can I avoid that? must I quite artificially use a different logging level to accomplish something like that with logback?

<configuration>

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logs/activity.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <fileNamePattern>activity.%i.log.zip</fileNamePattern>
      <minIndex>1</minIndex>
      <maxIndex>10</maxIndex>
    </rollingPolicy>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <maxFileSize>10MB</maxFileSize>
    </triggeringPolicy>
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

  <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
    <!-- use discarding threshold of zero to avoid ignoring INFO level messages see docs -->
    <discardingThreshold>0</discardingThreshold>
      <appender-ref ref="FILE" />
  </appender>

  <root level="info">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="ASYNC" />
  </root>

  <logger name="data-logger" level="info">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="ASYNC" />
  </logger>

</configuration>
like image 587
matanster Avatar asked Jun 10 '16 10:06

matanster


People also ask

What is additivity in Logback?

1.2 Logback AdditivityAppenders are added to the loggers. One logger may include more than one appenders. Thus, its log messages are written more than one desired destination systems. Additivity is exactly about this point. The output of a log statement of logger A will go to all the appenders in A and its ancestors.

How do you filter Logback logs?

In logback-classic, filters can be added to Appender instances. By adding one or more filters to an appender, you can filter events by arbitrary criteria, such as the contents of the log message, the contents of the MDC, the time of day or any other part of the logging event.

Does Logback require slf4j?

Note that logback-classic transitively includes the slf4j-api and logback-core , so only having the logback-classic is enough to setup logback with slf4j. When using Logback with SLF4j, a console appender with DEBUG log level is configured automatically.

Does spring boot use Log4j instead of Logback?

Spring Boot supports Log4j 2 for logging configuration if it is on the classpath. If you are using the starters for assembling dependencies that means you have to exclude Logback and then include log4j 2 instead. If you aren't using the starters then you need to provide jcl-over-slf4j (at least) in addition to Log4j 2.


1 Answers

Loggers are hierarchical, and any message sent to a logger will be sent to all its ancestors by default. You can disable this behavior by setting additivity=false. E.g.:

<logger name="data-logger" level="info" additivity="false">
like image 198
Mureinik Avatar answered Sep 19 '22 18:09

Mureinik