How to Configure a Specific Appender or Pattern for the Lombok @Slf4j Annotation in Spring Boot
Use the topic attribute in @Slf4j(topic = "unique.logger.name") to assign a specific name to your Lombok-generated logger, then define a matching <logger> element in logback-spring.xml that references your custom appender and pattern.
The Lombok @Slf4j annotation is a compile-time convenience that injects an SLF4J Logger field into your classes. However, the actual logging implementation—including appenders, layouts, and patterns—is managed by Spring Boot's logging subsystem, which defaults to Logback. Understanding how to bridge these two systems allows you to route Lombok-generated log output to specific destinations with custom formatting.
How the Lombok @Slf4j Annotation Integrates with Spring Boot
Lombok's @Slf4j annotation generates a private static final Logger log field by calling LoggerFactory.getLogger(Class). By default, the logger name is the fully qualified class name. However, Spring Boot's LogbackLoggingSystem (located in core/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java) controls the runtime behavior.
During initialization, LogbackLoggingSystem invokes LogbackLoggingSystemProperties.apply() to translate Spring Boot properties (such as logging.pattern.console) into system properties that Logback can reference. These mappings are defined in LoggingSystemProperty.java, which includes constants like CONSOLE_PATTERN and FILE_PATTERN.
Step 1: Name Your Logger Using the Topic Attribute
To target a Lombok-generated logger with a specific configuration, you must first give it a unique name using the topic attribute:
package com.example.demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j(topic = "my.custom.logger")
public class MyService {
public void process() {
log.info("Processing started");
log.debug("Processing details");
log.info("Processing completed");
}
}
This generates LoggerFactory.getLogger("my.custom.logger"), creating a logger with the exact name my.custom.logger that you can reference in XML configuration.
Step 2: Configure a Specific Appender in logback-spring.xml
Place your logback-spring.xml in src/main/resources. Spring Boot's LogbackLoggingSystem specifically looks for logback-spring.xml before logback.xml (as defined in getStandardConfigLocations()), allowing you to use Spring Boot-specific features.
Defining the Appender
Define an appender with your custom pattern:
<appender name="MY_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
Binding the Logger
Reference the Lombok topic name in a logger element:
<logger name="my.custom.logger" level="INFO" additivity="false">
<appender-ref ref="MY_APPENDER"/>
</logger>
Setting additivity="false" prevents the messages from propagating to the root logger and being logged twice.
Step 3: Leverage Spring Boot Properties for Externalized Configuration
You can control levels and global patterns through application.yml or application.properties. The LogbackLoggingSystemProperties class applies these during startup:
logging:
level:
my.custom.logger: DEBUG
pattern:
console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n"
These properties set the system properties defined in LoggingSystemProperty.java (such as CONSOLE_PATTERN), which you can reference in your XML using ${CONSOLE_LOG_PATTERN} if desired.
Complete Implementation Example
Here is a full working example that demonstrates the integration:
MyService.java:
package com.example.demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j(topic = "business.operations")
public class MyService {
public void execute() {
log.info("Executing business logic");
log.debug("Internal state details");
}
}
logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="BUSINESS_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %highlight(%-5level) %cyan(%logger{36}) - %msg%n</pattern>
</encoder>
</appender>
<logger name="business.operations" level="DEBUG" additivity="false">
<appender-ref ref="BUSINESS_APPENDER"/>
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
When the application starts, Spring Boot's LogbackLoggingSystem loads this configuration, and the Lombok-generated logger writes exclusively to BUSINESS_APPENDER with the custom pattern.
Summary
- Lombok generates the logger, but Spring Boot controls the configuration through Logback.
- Use
@Slf4j(topic = "unique.name")to create a named logger that can be targeted in XML configuration. - Define custom appenders and patterns in
logback-spring.xmland bind them to your named logger using the<logger>element withadditivity="false". - Externalize level and pattern settings via
application.ymlproperties, which are applied byLogbackLoggingSystemPropertiesduring startup. - Reference
LogbackLoggingSystem.javaandLoggingSystemProperty.javain thespring-projects/spring-bootrepository to understand the underlying property mapping mechanism.
Frequently Asked Questions
Can I configure a Lombok @Slf4j logger without using the topic attribute?
Yes, but you must reference the fully qualified class name in your logback-spring.xml. Without the topic attribute, Lombok generates LoggerFactory.getLogger(MyService.class), making the logger name equal to the class path (e.g., com.example.MyService). Using topic is recommended because it provides a stable identifier that survives class refactoring and clearly indicates the logger's purpose in configuration files.
Does Spring Boot automatically apply logging properties to Lombok-generated loggers?
Yes. Spring Boot's LogbackLoggingSystem initializes the logging framework during application startup, before any application beans are created. It calls LogbackLoggingSystemProperties.apply() to set system properties for patterns and levels based on your application.yml or application.properties. These settings affect all SLF4J loggers, including those generated by Lombok, because they modify the underlying Logback configuration shared across the entire application.
Where should I place the logback-spring.xml file in my project?
Place logback-spring.xml in src/main/resources. According to the getStandardConfigLocations() method in LogbackLoggingSystem.java, Spring Boot searches for logback-spring.xml before logback.xml. Using the -spring variant is recommended because it allows you to use Spring Boot-specific features such as profile-specific configuration (<springProfile>) and property placeholders (${spring.application.name}).
Can I use application.properties to set a different pattern for a specific logger only?
No. Spring Boot's property-based configuration (logging.pattern.console and logging.pattern.file) applies globally to the corresponding appenders. To assign a unique pattern to a specific logger, you must define a separate appender in logback-spring.xml with its own <pattern> element and reference it in a <logger> element matching your Lombok topic name. You can still use properties to control the level via logging.level.my.custom.logger=DEBUG, but patterns require XML configuration for per-logger customization.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →