Configuring Logback Logging in Spring Boot: XML, Properties, and Code Examples
This article provides a comprehensive guide to configuring Logback in Spring Boot, covering Maven dependencies, SLF4J usage, step‑by‑step XML setup, application.yml integration, controller logging code, Lombok shortcuts, appender types, logger definitions, log levels, rolling policies, filters, and best practices for backend development.
Spring Boot uses Logback as its default logging framework; you can enable it directly when generating a project or add SLF4J and Logback dependencies manually:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>SLF4J acts as a simple façade allowing you to switch the underlying logging implementation without changing your code. After adding the dependencies, you can log with LoggerFactory.getLogger(...) and logger.info(...).
Quick implementation steps :
Create logback.xml under src/main/resources with a console appender ( STDOUT) and a rolling file appender ( fileLog) that writes to ${logFile}.log and defines pattern, charset, and rolling policy.
In application.yml set logging.config: classpath:logback.xml to point Spring Boot to the configuration file.
Add logging code to a controller, e.g.:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
private final Logger logger = LoggerFactory.getLogger(TestController.class);
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String logTest(String name, String age) {
logger.info("logTest,name:{},age:{}", name, age);
return "success";
}
}If Lombok is available, the controller can be simplified with @Slf4j:
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class TestController {
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String logTest(String name, String age) {
log.info("logTest,name:{},age:{}", name, age);
return "success";
}
}When the application runs, logs appear on the console and are written to logs/mutest.log as configured.
Logback XML structure : <configuration> – root element with optional attributes scan, scanPeriod, debug. <property> – defines global variables such as logFile and maxFileSize. <appender> – defines how logs are output. Types include ConsoleAppender, FileAppender, and RollingFileAppender, each with its own sub‑elements ( file, append, encoder, rollingPolicy, filter, etc.). <logger> – optional logger for a specific package or class, specifying name, level, additivity, and one or more appender-ref entries. <root> – mandatory root logger that sets the default level and references the primary appenders.
Log levels are TRACE < DEBUG < INFO < WARN < ERROR. The root logger defaults to DEBUG if not set; you can override it in logback.xml or via application.yml using logging.level entries.
Rolling policies control log file rotation. Common policies are: TimeBasedRollingPolicy – rotates based on time (e.g., daily) and can keep a maxHistory of archived files. SizeBasedTriggeringPolicy – rotates when a file reaches a size limit (e.g., 30 MB). SizeAndTimeBasedRollingPolicy – combines both time and size criteria.
Each policy uses <fileNamePattern> to define the naming scheme, often including ${logFile}, a date pattern, and %i for index when multiple files are created on the same day.
Filters let you include or exclude events. Examples: LevelFilter – deny a specific level (e.g., WARN) while accepting others. ThresholdFilter – only allow events at or above a given level (e.g., INFO). EvaluatorFilter – uses a boolean expression (e.g., message.contains("success")) to decide acceptance.
Finally, integrate Logback with Spring Boot’s application.yml by specifying the config file and optional per‑package log levels, for example:
logging:
config: classpath:logback.xml
level:
com.example.demo.dao: traceThis tutorial demonstrates a complete, production‑ready logging setup for a Spring Boot backend application.
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.
