Master Spring Boot Logging with SLF4J: Configuration, Levels, and Best Practices

This guide explains how Spring Boot handles logging through Commons Logging, recommends using SLF4J as a facade, shows code examples, details log elements, demonstrates how to enable DEBUG, configure console colors, file output, rolling policies, level control, and custom formats for effective backend logging.

Programmer DD
Programmer DD
Programmer DD
Master Spring Boot Logging with SLF4J: Configuration, Levels, and Best Practices

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 pattern that abstracts the underlying logging framework, allowing you to write code against the facade while the actual implementation (Logback, Log4j, etc.) handles the logging.

@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 and the @Slf4j annotation in pom.xml, a log object is created, and the actual output destination (console or file) depends on the logging framework configured in the Spring Boot project, defaulting to Logback.

Log Elements

A typical Spring Boot 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 World

Enable DEBUG Logging

You can switch to DEBUG level in two ways:

Add the --debug flag when running the jar, e.g., java -jar myapp.jar --debug.

Set debug=true in application.properties.

Enabling DEBUG only affects core loggers (embedded container, Hibernate, Spring, etc.); your own application logs at DEBUG level will still not appear unless explicitly configured.

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 ; Spring Boot 2.x defaults to DETECT , so no change is needed for most users.

File Output

Spring Boot logs only to the console by default. To write logs to a file, add the following to application.properties:

logging.file.name=run.log
logging.file.path=./

File Rolling

Logback supports file rolling with these properties: 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 to keep (default 7). logging.logback.rollingpolicy.max-file-size: maximum size before rolling (default 10MB). logging.logback.rollingpolicy.total-size-cap: total size cap for all archives (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, or OFF. logging.level.com.didispace=DEBUG – all classes under com.didispace log at DEBUG. logging.level.root=WARN – root logger logs at WARN.

After configuring, the previously hidden DEBUG Hello World message will appear.

Custom Log Configuration Files

Log configuration files are loaded before the ApplicationContext is created, so you can also use system properties or external configuration files. 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 Format

Define console and file patterns in application.properties: logging.pattern.console: pattern for console output (not supported for JDK Logger). logging.pattern.file: pattern for file output (not supported for JDK Logger).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Configurationlogginglogbackslf4jfile-outputspring-bootdebug
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.