Skip to content

Instantly share code, notes, and snippets.

@aoudiamoncef
Last active June 12, 2024 10:11
Show Gist options
  • Save aoudiamoncef/8863b4efacad7497120fdd83ef216d43 to your computer and use it in GitHub Desktop.
Save aoudiamoncef/8863b4efacad7497120fdd83ef216d43 to your computer and use it in GitHub Desktop.
Spring Boot Logback Async Logging

Spring Boot Logback Async Logging

Overview

This gist demonstrates configuring asynchronous logging with Logback in a Spring Boot application. This approach provides improved performance, reduced latency, scalability, and fault tolerance.

By leveraging existing Spring logging configuration properties, we can customize the logging behavior according to your application's requirements seamlessly based on the envirement variables declared in defaults.xml. See Custom log configuration

Important

Contrary to the default configuration, file logging (ASYNC_FILE) is enabled even if logging.file.name or logging.file.path are not used.

Configuration

Logging configuration is defined in logback-spring.xml within src/main/resources. The AsyncAppender wraps both console and file appenders to enable asynchronous logging, enhancing performance and fault tolerance.

Here's a list of AsyncAppender parameters and their default values:

  1. queueSize: Maximum capacity of the blocking queue. Default value is 256.

  2. discardingThreshold: By default, drops events of TRACE, DEBUG, INFO when the queue has 20% capacity remaining; set to 0 to keep all events.

  3. includeCallerData: Exclude expensive caller data for better performance. Default value is false.

  4. maxFlushTime: Maximum time in milliseconds the AsyncAppender waits for the queue to flush during shutdown. Default value is not set.

  5. neverBlock: Appender blocks on full queue (default) or drops message if true. Default value is false.

These parameters can be configured within the AsyncAppender element in the Logback configuration file to customize the behavior of asynchronous logging.

References

logging:
file:
name: tutorial
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss.SSSZZ} %magenta([%thread]) [%logger{36}] %highlight(%level) %cyan([%class{0}.%method:%line]) - %message%n%xException}"
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" >
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<!-- Include default Spring Boot Logback configurations -->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<!-- Define the log file location -->
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<!-- Include default CONSOLE and FILE appenders -->
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<appender name="ASYNC_CONSOLE" class="ch.qos.logback.classic.AsyncAppender">
<!-- All available configurations and default values are shown -->
<appender-ref ref="CONSOLE" />
<queueSize>256</queueSize>
<!-- <discardingThreshold></discardingThreshold> -->
<includeCallerData>false</includeCallerData>
<!-- <maxFlushTime></maxFlushTime> -->
<neverBlock>false</neverBlock>
</appender>
<appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE"/>
</appender>
<!--
Configure the root logger to use your custom appenders.
Choose between console logging (ASYNC_CONSOLE) and file logging (ASYNC_FILE) based on your specific use case and requirements.
-->
<root level="INFO">
<appender-ref ref="ASYNC_CONSOLE" />
<!-- Contrary to the default configuration, ASYNC_FILE logging is enabled even if logging.file.name or logging.file.path is not used -->
<appender-ref ref="ASYNC_FILE" />
</root>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment