Understanding Spring Boot Default Logging Configuration and Customization
This article explains Spring Boot's default logging setup using SLF4J and Logback, demonstrates how to view log levels in a test class, shows how to modify log levels via application properties, and details the underlying XML configuration files and how to customize them for different environments.
Spring Boot uses SLF4J with Logback as its default logging framework. The article starts with a simple JUnit test class that logs messages at various levels (trace, debug, info, warn, error) to illustrate that the default root level is INFO.
package com.staticzz.springboot_logging;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootLoggingApplicationTests {
/**
* SLF4J日志记录器
*/
Logger logger = LoggerFactory.getLogger(getClass());
@Test
public void contextLoads() {
/**
* 日志级别,由低到高
* 我们可以调整日志级别,日志就只会在这个级别以后生效
*/
logger.trace("这是trace日志");
logger.debug("这是debug信息");
logger.info("这是info信息");
/**
* 运行后,从info信息开始输出,说明Springboot默认是info级别的日志信息
*/
logger.warn("这是warning信息");
logger.error("这是error信息");
}
}To change the logging level, you can add entries to application.properties, for example logging.level.com.staticzz=trace, which sets all classes in that package to TRACE level. The article also explains that logging.file and logging.path are mutually exclusive. logging.level.com.staticzz=trace The default Logback configuration is composed of several XML files bundled with Spring Boot: base.xml, defaults.xml, console-appender.xml, and file-appender.xml. The base.xml includes the other files and defines the root logger level as INFO.
<?xml version="1.0" encoding="UTF-8"?>
<!--
Base logback configuration provided for compatibility with Spring Boot 1.1
-->
<included>
<!--引入了默认配置的xml文件-->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<!--引入了控制台输出格式的xml文件-->
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<!--引入了文件输出格式的xml文件-->
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<!-- Springboot日志默认级别的配置 -->
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</included> defaults.xmldefines conversion rules, default log patterns, and a set of logger level overrides for common libraries.
<?xml version="1.0" encoding="UTF-8"?>
<!--
Default logback configuration provided for import, equivalent to the programmatic
initialization performed by Boot
-->
<included>
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
<!-- logger level overrides -->
<logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/>
<logger name="org.apache.catalina.util.LifecycleBase" level="ERROR"/>
<logger name="org.apache.coyote.http11.Http11NioProtocol" level="WARN"/>
...
</included> console-appender.xmlconfigures the console appender to use the pattern defined by the CONSOLE_LOG_PATTERN property.
<included>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
</included> file-appender.xmlconfigures a rolling file appender with a size‑based trigger and a pattern defined by FILE_LOG_PATTERN.
<included>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
</included>The environment variables referenced in the patterns are set by LoggingSystemProperties, which extracts values from the Spring Environment and populates system properties such as CONSOLE_LOG_PATTERN, FILE_LOG_PATTERN, and LOG_LEVEL_PATTERN.
package org.springframework.boot.logging;
import org.springframework.boot.ApplicationPid;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.core.env.Environment;
/**
* Utility to set system properties that can later be used by log configuration files.
*/
class LoggingSystemProperties {
static final String PID_KEY = LoggingApplicationListener.PID_KEY;
static final String EXCEPTION_CONVERSION_WORD = LoggingApplicationListener.EXCEPTION_CONVERSION_WORD;
static final String CONSOLE_LOG_PATTERN = LoggingApplicationListener.CONSOLE_LOG_PATTERN;
static final String FILE_LOG_PATTERN = LoggingApplicationListener.FILE_LOG_PATTERN;
static final String LOG_LEVEL_PATTERN = LoggingApplicationListener.LOG_LEVEL_PATTERN;
private final Environment environment;
LoggingSystemProperties(Environment environment) {
this.environment = environment;
}
public void apply() {
apply(null);
}
public void apply(LogFile logFile) {
RelaxedPropertyResolver propertyResolver = RelaxedPropertyResolver
.ignoringUnresolvableNestedPlaceholders(this.environment, "logging.");
setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD, "exception-conversion-word");
setSystemProperty(PID_KEY, new ApplicationPid().toString());
setSystemProperty(propertyResolver, CONSOLE_LOG_PATTERN, "pattern.console");
setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file");
setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level");
if (logFile != null) {
logFile.applyToSystemProperties();
}
}
private void setSystemProperty(RelaxedPropertyResolver propertyResolver,
String systemPropertyName, String propertyName) {
setSystemProperty(systemPropertyName, propertyResolver.getProperty(propertyName));
}
private void setSystemProperty(String name, String value) {
if (System.getProperty(name) == null && value != null) {
System.setProperty(name, value);
}
}
}For custom logging, Spring Boot recommends placing a logback‑spring.xml file on the classpath. Unlike a plain logback.xml, this file is processed by Spring Boot, allowing the use of springProfile sections to activate configuration only for specific profiles.
<springProfile name="profile属性">
<!-- configuration to be enabled when this profile is active -->
</springProfile>Overall, the article provides a comprehensive view of Spring Boot's default logging setup and practical steps to customize log levels, output destinations, and format, as well as how to leverage profile‑specific configurations.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
