Master Spring Boot Logging: From SLF4J Basics to Advanced Configuration
This tutorial explains how Spring Boot handles logging with Commons Logging, demonstrates using SLF4J as a facade, shows a complete code example, and covers configuring log output, levels, color, file rotation, and custom patterns for effective backend logging.
Spring Boot uses Commons Logging for all internal logs, but the underlying implementation is pluggable; common frameworks such as Java Util Logging, Log4J2, and Logback have automatic configuration components, and the default logger when using starters is Logback.
How to Log
Instead of worrying about whether Logback or Log4j is used, directly use SLF4J . SLF4J (Simple Logging Facade for Java) is a facade that abstracts the actual logging implementation, delegating to Logback, Log4j, or other frameworks at runtime.
@Slf4j
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
log.error("Hello World");
log.warn("Hello World");
log.info("Hello World");
log.debug("Hello World");
log.trace("Hello World");
}
}By adding Lombok's @Slf4j annotation in pom.xml, a log object is generated, allowing easy logging; the actual destination (console or file) depends on the logging framework configured in the Spring Boot project, defaulting to Logback.
Log Elements
When a Spring Boot application starts, each log line contains seven elements:
Timestamp (millisecond precision)
Log level (ERROR, WARN, INFO, DEBUG, TRACE)
Process ID
Separator --- indicating the start of the actual log
Thread name (enclosed in brackets)
Logger name (usually the class name)
Log message
Log Output
By default, Spring Boot logs to the console with levels ERROR, WARN, and INFO. Running the Hello World example produces output like:
2021-12-28 17:37:25.578 INFO 65136 --- [ main] com.didispace.chapter81.Application : Started Application in 2.695 seconds (JVM running for 3.957)
2021-12-28 17:37:25.579 ERROR 65136 --- [ main] com.didispace.chapter81.Application : Hello World
2021-12-28 17:37:25.579 WARN 65136 --- [ main] com.didispace.chapter81.Application : Hello World
2021-12-28 17:37:25.579 INFO 65136 --- [ main] com.didispace.chapter81.Application : Hello WorldEnable DEBUG Logging
Two ways to switch to DEBUG level:
Add the --debug flag when running the jar, e.g., java -jar myapp.jar --debug.
Set debug=true in application.properties.
Enabling DEBUG affects core loggers (embedded container, Hibernate, Spring, etc.) but does not automatically output your own application's DEBUG logs, as shown in the screenshot.
Log Configuration
Colorful Output
If your terminal supports ANSI, enable colored output by setting spring.output.ansi.enabled in application.properties to NEVER, DETECT (default), or ALWAYS.
Note: Spring Boot 1.x defaults to NEVER , while 2.x defaults to DETECT , so most users see colored logs without changes.
File Output
Spring Boot logs only to the console by default. To write logs to a file, add in application.properties:
logging.file.name=run.log
logging.file.path=./In Spring Boot 1.x the corresponding properties are logging.file and logging.path.
File Rolling
Logback supports file rolling. Key properties include: logging.logback.rollingpolicy.file-name-pattern: pattern for archived log filenames. logging.logback.rollingpolicy.clean-history-on-start: whether to clean old archives on startup (default false). logging.logback.rollingpolicy.max-history: maximum number of archived files (default 7). logging.logback.rollingpolicy.max-file-size: maximum size before rolling (default 10MB). logging.logback.rollingpolicy.total-size-cap: total size cap before deletion (default 0B).
Level Control
Control logger levels via application.properties using the pattern logging.level.*=LEVEL, where * is the package or logger name and LEVEL can be TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. logging.level.com.didispace=DEBUG sets all classes under com.didispace to DEBUG. logging.level.root=WARN sets the root logger to WARN.
After configuring, your own DEBUG logs will appear as expected.
Custom Log Configuration Files
Log configuration can be placed in external files loaded before the ApplicationContext is created. Use the following naming conventions:
Logback: logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy Log4j2: log4j2-spring.xml, log4j2.xml JDK (Java Util Logging): logging.properties Spring Boot recommends using files with the -spring suffix (e.g., logback-spring.xml) for better integration.
Custom Output Pattern
Define console and file output patterns in application.properties using logging.pattern.console and logging.pattern.file respectively (not supported for JDK Logger).
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
