Search code examples
javalogginglogbackslf4j

Is there an off-the-shelf way to make Sfl4j / Logback to log a message only when it changes?


My java app depends on a 3rd party library. This library logs with Slf4j. When the logger corresponding to the package of the library is set to INFO this kind of log is produced:

14:20:49.736 [scheduling-1] INFO  i.i.s.m.a.m.MeterRegistryService - collectNumberOfApplications - Publishing the number of active applications '1' and disabled ones '0'.
14:20:54.739 [scheduling-1] INFO  i.i.s.m.a.m.MeterRegistryService - collectNumberOfApplications - Publishing the number of active applications '1' and disabled ones '0'.
14:20:59.742 [scheduling-1] INFO  i.i.s.m.a.m.MeterRegistryService - collectNumberOfApplications - Publishing the number of active applications '1' and disabled ones '0'.
14:21:04.745 [scheduling-1] INFO  i.i.s.m.a.m.MeterRegistryService - collectNumberOfApplications - Publishing the number of active applications '1' and disabled ones '0'.
14:21:09.748 [scheduling-1] INFO  i.i.s.m.a.m.MeterRegistryService - collectNumberOfApplications - Publishing the number of active applications '1' and disabled ones '0'.

So I was wondering if there is some slf4j or logback built-in feature that would log this line only if the body of the message changes compared to the previous line. I know I probably can do it with some custom made appender, or extending some already esisting component, keeping track of the hashes of the previous n messages written by a certain logger and actually log only if the the logger is writing the first occurrence of a message, but I would prefer an already made solution if available.


Solution

  • In logback there is an out of the box filter that you can use for this called DuplicateMessageFilter. This filters out exact duplicate messages. You can make use of a specified threshold on how many repititions you want (through the allowedRepititions property)

    Just add this filter in your logback configuration and the duplicates should be removed.

    Example of the logback config:

    <configuration>
    
      <turboFilter class="ch.qos.logback.classic.turbo.DuplicateMessageFilter" allowedRepetitions="1"/>
    
      <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
          <pattern>%date [%thread] %-5level %logger - %msg%n</pattern>
        </encoder>
      </appender>
    
      <root level="INFO">
        <appender-ref ref="console" />
      </root>  
    </configuration>
    

    More info: https://logback.qos.ch/manual/filters.html#DuplicateMessageFilter