Skip to content

Instantly share code, notes, and snippets.

@tggreene
Last active April 15, 2022 20:58
Show Gist options
  • Save tggreene/27ad3b6e9a8d97f037cef48004cdc208 to your computer and use it in GitHub Desktop.
Save tggreene/27ad3b6e9a8d97f037cef48004cdc208 to your computer and use it in GitHub Desktop.
Logback Explainer
<!-- Logback Configuration. See http://logback.qos.ch/ -->
<!-- Adapted from https://github.com/stuartsierra/log.dev/blob/master/resources/logback.xml -->
<!-- For development (or even prod) you may want scan behaviour to pick up changes dynamically
<configuration scan="true" scanPeriod="1 seconds">
You can always modify runtime log levels with:
(.setLevel (org.slf4j.LoggerFactory/getLogger "your.namespace")
(ch.qos.logback.classic.Level/valueOf "INFO"))
-->
<configuration>
<property resource="log_dev_app.properties" />
<!-- Console (STDOUT) output. -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<!-- Only print log messages at level WARN or higher. -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<!-- Default encoder is ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<!-- two-line layout suitable for a terminal -->
<pattern>%date{HH:mm:ss.SSS} %highlight(%-5level) %logger [%thread]%n%msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- For production we typically want our logs json encoded and emitted to the console
(assuming we have some runtime that collects and ships these logs for us).
We need to make sure that we have the logstash encoder dependency available for this
encoder:
e.g. net.logstash.logback/logstash-logback-encoder {:mvn/version "6.6"}
-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>
<!-- Alternatively we can use logback-jackson from the contrib (although appears to be less
frequently used than the logstash encoder
ch.qos.logback.contrib/logback-jackson {:mvn/version "0.1.5"}
-->
<appender name="JSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSX</timestampFormat>
<timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>
<jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>true</prettyPrint>
</jsonFormatter>
</layout>
</encoder>
</appender>
<!-- Example of a rolling file appender -->
<!-- The output file configuration for log/all.log -->
<appender name="ALL_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- Default encoder is ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%date{HH:mm:ss.SSS} %-5level %logger{25}: %msg %X thread=%thread%n</pattern>
</encoder>
<!-- Default location of log file is log/all.log -->
<file>log/all.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- Roll over log files daily -->
<fileNamePattern>log/all.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- And start a new file every 64 MB -->
<maxFileSize>64 MB</maxFileSize>
<!-- Keep at most 15 days of history -->
<maxHistory>15</maxHistory>
<!-- Up to a maximum of 512 MB -->
<totalSizeCap>512MB</totalSizeCap>
<!-- Ensure short-lived processes still clean up old logs -->
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
</appender>
<!-- The output file configuration for log/app.log -->
<appender name="APP_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- Default encoder is ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%date{HH:mm:ss.SSS} %-5level %logger{25}: %msg %X thread=%thread%n</pattern>
</encoder>
<!-- Default location of log file is log/app.log -->
<file>log/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- Roll over log files daily -->
<fileNamePattern>log/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- And start a new file every 64 MB -->
<maxFileSize>64 MB</maxFileSize>
<!-- Keep at most 15 days of history -->
<maxHistory>15</maxHistory>
<!-- Up to a maximum of 512 MB -->
<totalSizeCap>512MB</totalSizeCap>
<!-- Ensure short-lived processes still clean up old logs -->
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
</appender>
<!-- Root log level is "ALL", meaning all log levels are emitted. -->
<!-- It's useful to have a LOG_LEVEL environment override so you can quickly toggle dynamically -->
<!-- Don't use filters in appenders for this unless you have some canonical appender with no filter set i.e. you can find it somewhere -->
<!-- Children _can_ override parents, so setting the base log level you're interested in here is sensible -->
<root level="${LOG_LEVEL:-ALL}">
<!-- Send all log messages to console (filtered to WARN) -->
<appender-ref ref="CONSOLE" />
<!-- Send all log messages to log/all.log -->
<appender-ref ref="ALL_LOG_FILE" />
</root>
<!-- Log messages from your application will be included in
log/all.log. In addition, we will send just messages from your
application to log/app.log -->
<logger name="${app_root_logger:-com.example.application}" level="ALL">
<appender-ref ref="APP_LOG_FILE" />
</logger>
<!-- If you have a REPL or interactive shell with a logger named
'user' or 'dev', send those messages to log/app.log too. -->
<logger name="user" level="ALL">
<appender-ref ref="APP_LOG_FILE" />
</logger>
<logger name="dev" level="ALL">
<appender-ref ref="APP_LOG_FILE" />
</logger>
<!-- Prevents irritating WARN level messages about profile prefixes from aws sdk -->
<logger name="com.amazonaws.auth.profile.internal.BasicProfileConfigLoader" level="ERROR">
<appender-ref ref="CONSOLE" />
</logger>
<!-- Make java.util.logging more efficient at disabled levels.
See http://logback.qos.ch/manual/configuration.html#LevelChangePropagator -->
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment