Logback for SpringBoot

logback

1. Initial Setup

@RestController
public class LoggingController {

    Logger logger = LoggerFactory.getLogger(LoggingController.class);

    @RequestMapping("/")
    public String index() {
        logger.trace("A TRACE Message");
        logger.debug("A DEBUG Message");
        logger.info("An INFO Message");
        logger.warn("A WARN Message");
        logger.error("An ERROR Message");

        return "Howdy! Check out the Logs to see the output...";
    }
}

Once we’ve loaded the web application, we’ll be able to trigger those logging lines by simply visiting http://localhost:8080/.

2. Zero Configuration Logging

关于 jar 包依赖

We shouldn’t worry about importing spring-jcl at all if we’re using a Spring Boot Starter (which we almost always are). That’s because every starter, like our spring-boot-starter-web, depends on spring-boot-starter-logging, which already pulls in spring-jcl for us.

2.1 Default Logback Logging

When using starters, Logback is used for logging by default.

Spring Boot preconfigures it with patterns and ANSI colors to make the standard output more readable.

As we can see, the default logging level of the Logger is preset to INFO, meaning that TRACE and DEBUG messages are not visible.

In order to activate them without changing the configuration, we can pass the –debug or –trace arguments on the command line:

java -jar target/demo-1.0.jar --debug

2.2 Log Levels

Spring Boot also gives us access to a more fine-grained log level setting.

First, we can set our logging level within our VM Options:

-Dlogging.level.org.springframework=INFO
-Dlogging.level.com.baeldung=DEBUG

Alternatively, if we’re using Maven, we can define our log settings via the command line:

mvn spring-boot:run
  -Dspring-boot.run.arguments=--logging.level.org.springframework=INFO,--logging.level.com.baeldung=DEBUG

If we want to change the verbosity permanently, we can do so in the application.properties file as described here:

logging.level.root=INFO
logging.level.com.baeldung=DEBUG

Finally, we can change the logging level permanently by using our logging framework configuration file.

We mentioned that Spring Boot Starter uses Logback by default. Let’s see how to define a fragment of a Logback configuration file in which we set the level for two separate packages:

<logger name="org.springframework" level="INFO" />
<logger name="com.baeldung" level="DEBUG" />

这几个选项,最终日志级别会是最低的

3. Logback Configuration

Let’s see how to include a Logback configuration with a different color and logging pattern, with separate specifications for console and file output, and with a decent rolling policy to avoid generating huge log files.

When a file in the classpath has one of the following names, Spring Boot will automatically load it over the default configuration:

  • logback-spring.xml

  • logback.xml

  • logback-spring.groovy

  • logback.groovy

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property name="LOGS" value="./logs" />

    <appender name="Console"
        class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFile"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS}/spring-boot-logger.log</file>
        <encoder
            class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
        </encoder>

        <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily and when the file reaches 10 MegaBytes -->
            <fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>
    
    <!-- LOG everything at INFO level -->
    <root level="info">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </root>

    <!-- LOG "com.baeldung*" at DEBUG level -->
    <logger name="com.baeldung" level="trace" additivity="false">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </logger>

</configuration>

the overall console pattern is both textually and chromatically different than before.

It also now logs on a file in a /logs folder created under the current path and archives it through a rolling policy.

4. Log4j2 Configuration Logging

All the routings to the other logging libraries are already included to make it easy to switch to them.

In order to use any logging library other than Logback, though, we need to exclude it from our dependencies.

For every starter like this one (it’s the only one in our example, but we could have many of them):

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

参考资料 https://www.baeldung.com/spring-boot-logging

Last updated