Mastering Spring Boot Logging: From Basics to Advanced Configuration
Learn why logging is essential in Java applications, explore Spring Boot’s built‑in SLF4J framework, discover how to print, configure levels, persist logs, customize formats, manage file rotation, and simplify logging with Lombok, all through clear examples and practical guidance.
Logging Overview
Logging is familiar to us from Java SE where we use System.out.print to output logs. As projects become more complex, simple console prints are insufficient for monitoring, auditing, and data analysis, so dedicated logging frameworks are needed.
Uses of Logging
System Monitoring
Logs record system status, method response times, and can trigger alerts when thresholds are exceeded.
Data Collection
Statistics: Collect page views, unique visitors, clicks for analysis.
Recommendation Ranking: Record user behavior for recommendation algorithms.
Log Auditing
Logs provide evidence for security audits, illegal access detection, and tracing data modifications.
Logging in Spring Boot
Spring Boot outputs logs by default using the SLF4J facade backed by Logback. Unlike System.out.print, SLF4J provides richer information such as timestamps, thread names, and log levels.
Printing Logs
Obtain a logger via LoggerFactory.getLogger(YourClass.class) and use methods like info(), debug(), etc.
private static Logger logger = LoggerFactory.getLogger(LoggerController.class);Example controller method:
@RestController
public class LoggerController {
@RequestMapping("/logger")
public String logger() {
logger.info("--- log content ---");
return "Print log";
}
}SLF4J Facade
SLF4J is a facade that abstracts underlying logging implementations (Log4j, Logback, etc.). It allows switching the concrete framework without changing application code.
Log Levels
FATAL: System‑level fatal error. ERROR: High‑severity error. WARN: Warning. INFO: General information. DEBUG: Debug details. TRACE: Fine‑grained tracing.
Example of logging at all levels:
@RequestMapping("/printLog")
public String printLog() {
logger.trace("=====trace=====");
logger.debug("=====debug=====");
logger.info("=====info=====");
logger.warn("=====warn=====");
logger.error("=====error=====");
return "Print different level logs";
}Configuration
Setting Log Levels
In application.properties: logging.level.root=debug Or in application.yml:
logging:
level:
root: debugLog Persistence
Configure file name and path:
logging.file.name=logger/springboot.log logging.file.path=D:/tempOnly one of the above takes effect; logging.file.name overrides logging.file.path.
File Rotation
Set rotation policy, e.g., split when size exceeds 1KB for demonstration:
logging.logback.rollingpolicy.file-name-pattern=${LOG_FILE}.%d{yyyy-MM-dd}.%i
logging.logback.rollingpolicy.max-file-size=1KBLog Format
Console format ( logging.pattern.console) and file format ( logging.pattern.file) can be customized with placeholders such as %d for date, %5p for level, %t for thread, %c for logger name, and %m for message.
Simplified Logging with Lombok
Add Lombok dependency and annotate classes with @Slf4j to obtain a ready‑made log object.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> @Slf4j
@RestController
public class LogController {
public void log() {
log.info("===========log content===========");
}
}Summary
Logging is a crucial part of Java applications for problem detection, monitoring, auditing, and data analysis. Spring Boot provides a default SLF4J/Logback setup, and developers can control log levels, persistence, rotation, and formatting. Lombok further simplifies logger injection.
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.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.
