Skip to content

Instantly share code, notes, and snippets.

@aoudiamoncef
Last active July 4, 2019 15:47
Show Gist options
  • Save aoudiamoncef/05ea42f4ce78d354b5d3bca004e738b7 to your computer and use it in GitHub Desktop.
Save aoudiamoncef/05ea42f4ce78d354b5d3bca004e738b7 to your computer and use it in GitHub Desktop.
Spring Logback heavy JSON logging with size and time based rolling policy
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="APPLICATION_NAME" value="lahzouz"/>
<property name="LOG_PATH" value="/var/log/lahzouz/${APPLICATION_NAME}"/>
<property name="FILE_PREFIX" value="${APPLICATION_NAME}"/>
<property name="PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} %magenta([%thread]) %highlight(%level) %cyan(%logger{36}) - %message%n%xException"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder>
<pattern>
${PATTERN}
</pattern>
</encoder>
</appender>
<appender name="SAVE-TO-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/${FILE_PREFIX}.log</file>
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/archived/${FILE_PREFIX}_%d{yyyy-MM-dd HH-mm}.%i.zip</fileNamePattern>
<maxFileSize>100MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>2GB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="SAVE-TO-FILE"/>
</appender>
<springProfile name="local">
<root level="info">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="SAVE-TO-FILE"/>
</root>
<logger name="org.springframework.boot" additivity="false" level="debug">
<appender-ref ref="ASYNC"/>
</logger>
</springProfile>
<springProfile name="!local">
<root level="info">
<appender-ref ref="ASYNC"/>
</root>
<logger name="org.springframework.boot">
<appender-ref ref="ASYNC"/>
</logger>
</springProfile>
</configuration>
<properties>
...
<logstash-logback-encoder.version>6.1</logstash-logback-encoder.version>
<jansi.version>1.18</jansi.version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>${logstash-logback-encoder.version}</version>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>${jansi.version}</version>
</dependency>
...
</dependencies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment