Skip to content

Instantly share code, notes, and snippets.

@alexengrig
Created March 2, 2023 16:37
Show Gist options
  • Save alexengrig/87e0addf527858ca32983969a41b452b to your computer and use it in GitHub Desktop.
Save alexengrig/87e0addf527858ca32983969a41b452b to your computer and use it in GitHub Desktop.
Logback / Logstash / Fluentd
dependencies {
implementation 'net.logstash.logback:logstash-logback-encoder:7.2'
implementation 'org.fluentd:fluent-logger:0.3.4'
implementation 'com.sndyuk:logback-more-appenders:1.5.6'
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
Spring Boot Logback configurations:
https://github.com/spring-projects/spring-boot/tree/main/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback
STASH properties
stashFile = logging.stash.file (context)
| STASH_FILE (env)
| stashPath/stashFilename.log
stashPath = logging.stash.path (context)
| STASH_PATH (env)
| '.' (default)
stashFilename = logging.stash.filename (context)
| STASH_FILENAME (env)
| appName
appName = spring.application.name (context)
| APP_NAME (env)
| 'application' (default)
-->
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<springProperty
name="appName"
scope="context"
source="spring.application.name"
defaultValue="${APP_NAME:-application}"
/>
<!-- Stash for Logstash -->
<springProperty
name="stashPath"
scope="context"
source="logging.stash.path"
defaultValue="${STASH_PATH:-.}"
/>
<springProperty
name="stashFilename"
scope="context"
source="logging.stash.filename"
defaultValue="${STASH_FILENAME:-${appName}}"
/>
<springProperty
name="stashFile"
scope="context"
source="logging.stash.file"
defaultValue="${STASH_FILE:-${stashPath}/${stashFilename}.log}"
/>
<property name="STASH_FILE" value="${stashFile}"/>
<appender name="STASH" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
<file>${STASH_FILE}</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>
${LOGBACK_ROLLINGPOLICY_FILE_NAME_PATTERN:-${STASH_FILE}.%d{yyyy-MM-dd}.%i.gz}
</fileNamePattern>
<cleanHistoryOnStart>
${LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START:-false}
</cleanHistoryOnStart>
<maxFileSize>
${LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE:-10MB}
</maxFileSize>
<totalSizeCap>
${LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP:-0}
</totalSizeCap>
<maxHistory>
${LOGBACK_ROLLINGPOLICY_MAX_HISTORY:-7}
</maxHistory>
</rollingPolicy>
</appender>
<!-- Fluent for Fluentd -->
<property name="FLUENTD_HOST" value="${FLUENTD_HOST:-localhost}"/>
<property name="FLUENTD_PORT" value="${FLUENTD_PORT:-24224}"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
<!-- Check tag and label fluentd info: https://docs.fluentd.org/configuration/config-file -->
<tag>microservice.helloworld.access</tag>
<label>normal</label>
<remoteHost>${FLUENTD_HOST}</remoteHost>
<port>${FLUENTD_PORT}</port>
</appender>
<!-- Root -->
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="STASH"/>
<appender-ref ref="FLUENT"/>
</root>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment