How is the configuration of logback in Spring Boot done?

In Spring Boot, you can use Logback as the logging framework and configure it through a configuration file. Here is how you can configure Logback.

  1. pom.xml is the abbreviation for “Project Object Model” XML file.
<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>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
</dependency>
  1. the XML configuration file for logback in a Spring application
<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d %-5level [%thread] %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    
    <root level="info">
        <appender-ref ref="console" />
    </root>
</configuration>

In the aforementioned configuration file, an Appender named console for console output is defined, specifying the format of the log output. Subsequently, the console Appender is added to the root Logger and the log level is set to info.

  1. properties file for applications
  2. configurations file
  3. caution
logging.level.root=warn

By doing so, the configuration of Logback is completed. In the application, you can use the org.slf4j.Logger interface provided by Spring Boot to log messages. For example:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class MyService {
    private static final Logger logger = LoggerFactory.getLogger(MyService.class);
 
    public void doSomething() {
        logger.info("Doing something");
    }
}
广告
Closing in 10 seconds
bannerAds