Skip to content

Instantly share code, notes, and snippets.

@justone
Created February 13, 2020 17:51
Show Gist options
  • Save justone/12fa681f70064307c7db0e7dca4a55fa to your computer and use it in GitHub Desktop.
Save justone/12fa681f70064307c7db0e7dca4a55fa to your computer and use it in GitHub Desktop.

This logback configuration is an example of diverting debug level logs for a specific package to a separate file, while maintaining info level in the main log. This allows viewing all package logs in context, and then when an info or warn level log appears, you can switch to the debug log and see that line in context with debug statements.

This is particularly useful for debugging a new or troublesome package in production without cluttering up your main log stream.

The main appender is the primary appender to which all application logs are sent. The stream-debug appender is only referred by the app.component.stream package. The key configuration of the appenders is the ThresholdFilter. That ensures that even though the app.component.stream is elevated to debug level output, the main appender will only write out at the info level while the stream-debug appender will send debug level through.

<configuration scan="true" debug="false" scanPeriod="10 seconds">
<!-- Switch this with the OnConsoleStatusListener class to hide log information when reloading
<statusListener class="ch.qos.logback.core.status.NopStatusListener"/>
-->
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/>
<appender name="main" class="ch.qos.logback.core.FileAppender">
<file>app.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} | %-40thread | %-5level | %-20logger{20} | %msg%n</pattern>
</encoder>
</appender>
<appender name="stream-debug" class="ch.qos.logback.core.FileAppender">
<file>stream-debug.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} | %-40thread | %-5level | %-20logger{20} | %msg%n</pattern>
</encoder>
</appender>
<logger name="app" level="info">
<appender-ref ref="main"/>
</logger>
<logger name="app.component.stream" level="debug">
<appender-ref ref="stream-debug"/>
</logger>
<root level="none">
</root>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment