Why Your logback-spring.xml Isn’t Working and How to Fix It
This article walks through the complete troubleshooting process for a non‑functional logback‑spring.xml in a Spring Boot application, covering background, configuration steps, common pitfalls such as variable resolution and dependency conflicts, remote debugging techniques, and final recommendations to ensure proper log output.
Background
Recently a teammate reported that the logback-spring.xml file was not taking effect. Because the author had not created a new application for a long time, the configuration details were unfamiliar, leading to a lengthy investigation.
Logback Overview
SLF4J (Simple Logging Facade for Java) provides a unified logging API, while Logback is a logging implementation that integrates seamlessly with SLF4J and can replace it.
Logback Configuration
Pandora‑boot includes Logback by default, so no additional Logback dependency is required.
Configuration Approach
The official recommendation is to use Logback, and the troubleshooting steps focus on Logback not taking effect.
Specify Configuration File Path
In application.properties add:
logging.config=classpath:logback-spring.xmlCustom Configuration File
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<!-- defaults.xml from Spring Boot -->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="APP_NAME" value="xxxx" />
<property name="LOG_PATH" value="${user.home}/logs/eleme/${APP_NAME}/" />
<property name="LOG_FILE" value="${LOG_PATH}/application.log" />
<property name="ERROR_LOG_FILE" value="${LOG_PATH}/common-error.log"/>
<property name="TRACE_FILE_LOG_PATTERN" value="[%X{requestId}][%X{rpcId}] -%green(%d{yyyy-MM-dd HH:mm:ss.SSS}) ${LOG_LEVEL_PATTERN:-%5p} %magenta(${PID:- }) --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<property name="ERROR_FILE_LOG_PATTERN_D" value="${ERROR_FILE_LOG_PATTERN_D:-%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} [%X{requestId}] [%X{rpcId}][traceId:%X{EAGLEEYE_TRACE_ID}][%c{2}#%M] %logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<appender name="APPLICATION" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<encoder>
<pattern>${TRACE_FILE_LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="ERROR_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${ERROR_LOG_FILE}</file>
<encoder>
<pattern>${ERROR_FILE_LOG_PATTERN_D}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ERROR_LOG_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${TRACE_CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="APPLICATION" />
<appender-ref ref="ERROR_APPENDER" />
</root>
</configuration>Potential Configuration Issues
1) Custom variables not taking effect? Only a few built‑in variables (${PID}, ${LOG_FILE}, ${LOG_PATH}, ${LOG_EXCEPTION_CONVERSION_WORD}) work directly; others must be defined as <property> entries, e.g., APP_NAME .
2) Want more error details? Add the JVM option -Dlog4j.defaultInitOverride=true when starting the application.
Symptoms After Configuration
After applying the configuration, custom appenders may still be ignored, and logs are only written to service_stdout.log. This is because the startup script start_pandora_boot forces output to $SERVICE_OUT, overriding the custom logback-spring.xml settings.
Analysis of Non‑functionality
Remote Debugging
Enable remote debug with
JPDA_OPTS="-agentlib:jdwp=transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND"Start IDEA Remote JVM Debug session.
Inspecting Logback Appender
Run LoggerFactory.getLogger("ROOT") and examine the ROOT logger. The custom appender defined in logback-spring.xml may be missing, indicating it was overridden or given lower priority.
Investigating Log Conflicts
Exclude SLF4J-Log4j12 when using Logback.
Remove jcl-over-slf4j and commons-logging to avoid classpath conflicts.
Use a Dependency Analyzer to quickly locate and exclude conflicting artifacts.
Double‑Check
Run LoggerFactory.getLogger("ROOT") again and verify that the custom appender and file are correctly initialized.
Conclusion
1) Read the Spring Boot documentation carefully; the pitfalls described are not unique.
2) When adding third‑party libraries, follow the principle of minimal inclusion and use exclusion to prevent transitive conflicts.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
