Section 10: log management in SpringBoot

Posted by trauch on Sat, 12 Feb 2022 07:12:04 +0100

The default logging framework used by SpringBoot is logback. Spring boot starter contains the spring boot starter logging module. The log framework is logback. Therefore, we do not need to introduce the spring boot starter logging module separately.


SpringBoot default log format

Format description

  • Timestamp, accurate to milliseconds: 2021-11-13 14:35:20.298
  • logback log level The log levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL:INFO
  • Process ID:1414
  • Separator: the default is:---
  • Thread Name: [restartedMain]

Case demonstration

We can use @ Slf4j annotation here.

pom.xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
        <relativePath/> <!-- lookup parent from repository -->
    <description>Section 10:SpringBoot Log management in</description>




Print our log in the Controller

package com.rumenz.lession10.controller;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

 * @className: RumenzController
 * @description: TODO Class description
 * @author: Entry station rumenz com
 * @date: 2021/11/11

@Slf4j //Comments provided by lombok
public class RumenzController {

    //If you don't use lombok, you need to use the following code to get the log operation object
    //Logger log = LoggerFactory.getLogger(RumenzController.class);

    public String index(){

        log.trace("Trace journal...");
        log.debug("Debug journal...");"Info journal...");
        log.warn("Warn journal...");
        log.error("Error journal...");

        return "Entry station";

Start the Lession10Application and visit the browser View console

By default, logback prints the log to the console.

Output logback log to file

By default, SpringBoot will only output the log to the console and will not record it to a file. In the production environment, the log needs to be recorded to a file for archiving. If you need to output to a file, you can do so in application Set logging. In the properties configuration file file. Path or logging file. name

  • logging. file. Path (the lower version is logging.path) sets the storage path of the log, which will generate spring. Path in the set directory Log file.
  • logging. file. Name (the lower version is sets the log file path and log file name, which can be absolute path or relative path.

The two cannot be used at the same time. If they are configured at the same time, only logging file. Name takes effect. By default, when the log size reaches 10M, it will be truncated and a new log file will be generated. configuration

Start the Lession10Application and visit the browser View in the current directory Log will find the contents of the log. Of course, the console will also output.

How to set the log level

The log level is provided by TRACE < DEBUG < INFO < warn < error < fatal level by level. If the log is set to INFO, the logs of DEBUG and TRACE levels will not be output.

The default log level of SpringBoot is INFO. We change the default log level of SpringBoot to WARN


Start the Lession10Application and look at the console

root is all log levels of the project. The startup succeeded, but there was no log output. We set the default log level to WARN. According to the hierarchical relationship, the logs of info, debug and trace are not output.

We are in application Properties change the log level of root to INFO, and the log level of business package written by yourself to DEBUG

logging.level.root=INFO //The root log outputs information at the INFO level //Specify the classes under the config package to output at the WARN level

We output an INFO level log in the RumenzConfig configuration class.

public class RumenzConfig {

    public User user(){"This is info Level log");
        return new User(1, "");
class User{
    private Integer id;
    private String  name;


Start the Lession10Application, look at the console and find the log Info ("this is the info level log"); No output.

Set log level in batch

If we want to give it to com rumenz. lession10. Controller and com rumenz. lession10. controller. Service sets a unified log level.

application. For customer configuration in properties, packages are separated by English commas,com.rumenz.lession10.controller.service

Other configuration items of log

  • logging.config log configuration;
  • logging. logback. rollingpolicy. Max file size (logging. File. Max size for lower versions) maximum log file size;
  • logging. logback. rollingpolicy. Max history (lower version logging. File. Max History) maximum number of archived files;
  • logging. pattern. Log mode of console output;
  • logging.pattern.dateformat the date format of the log;
  • logging.pattern.file uses log mode by default
  • logging.pattern.level log level


Used to specify the format of console log output. can be configured

logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-6level %logger- %msg%n


Used to specify the format of log output in the file. can be configured

logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-6level %logger- %msg%n

Meanings of the above symbol parameters

  • %D {yyyy MM DD HH: mm: SS} - log output time
  • %thread -- the name of the process that outputs the log, which is very useful in Web applications and asynchronous task processing
  • %-6level -- log level, with 5 characters aligned to the left
  • %logger - the name of the log exporter
  • %msg -- log message
  • %n -- newline character of platform

Source code address of this summary:


  • Follow the [entry station] and reply to [1001] to get the quick reference manual of common linux commands
  • Follow the [entry station] reply [1003] to get the solution of LeetCode [java language implementation]
  • Pay attention to [entry station] and reply to [1004] to get the summary of Java basic core
  • Follow [entry site] and reply to [1009] to obtain Alibaba Java development manual

Topics: Spring Boot