Mastering Java Logging: SLF4J, Logback, and Spring Boot Configuration Guide

This article explains how to choose and integrate Java logging frameworks, use SLF4J with Logback, understand Spring Boot's default logging setup, customize log output, and switch between implementations such as Log4j2, providing practical code snippets and configuration examples.

Programmer DD
Programmer DD
Programmer DD
Mastering Java Logging: SLF4J, Logback, and Spring Boot Configuration Guide

1. Logging Frameworks

When developing a large system, you may start with simple System.out.println but soon need a logging framework to record runtime information. Common Java logging frameworks include JUL, JCL, JBoss-logging, Logback, Log4j, Log4j2, and SLF4J.

日志门面(日志的抽象层)

日志实现

JCL(Jakarta Commons Logging) SLF4J(Simple Logging Facade for Java)

jboss-logging

Log4j JUL(java.util.logging) Log4j2

Logback

Select a logging facade (e.g., SLF4J) on the left and a concrete implementation (e.g., Logback) on the right.

2. Using SLF4J

Import the SLF4J API and a concrete implementation such as Logback, then obtain a logger via LoggerFactory.getLogger(...) and log at various levels.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(HelloWorld.class);
        logger.info("Hello World");
    }
}
SLF4J and Logback diagram
SLF4J and Logback diagram

Each implementation has its own configuration file; after switching to SLF4J, the configuration file belongs to the chosen implementation.

3. Spring Boot Logging Relationship

Spring Boot uses SLF4J and Logback by default. Adding spring-boot-starter-logging brings these dependencies. Spring Boot also replaces other logging APIs (Commons-Logging, JBoss-logging, etc.) with SLF4J.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-logging</artifactId>
</dependency>
Spring Boot logging dependency diagram
Spring Boot logging dependency diagram

Key points:

Spring Boot logs via SLF4J + Logback.

Other logging frameworks are automatically routed through SLF4J.

Custom logging configuration can be provided via logback-spring.xml, logback.xml, log4j2-spring.xml, etc.

4. Customizing Log Output

Spring Boot’s default configuration logs to the console at INFO level. You can change the level, file location, and pattern using properties such as logging.level.*, logging.file, logging.path, logging.pattern.console, and logging.pattern.file.

logging.level.com.atguigu=trace
logging.path=/spring/log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n

logging.file

logging.path

Example

Description

(none)

(none)

Console only

my.log

(none)

my.log

Write to my.log file

(none)

/var/log

/var/log

Write to spring.log in the specified directory

5. Switching Logging Implementations

To replace Logback with Log4j2, exclude the default logging starter and add spring-boot-starter-log4j2 as a dependency.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
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.

JavaConfigurationloggingSpring Bootlogbackslf4j
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.