SpringBoot -- logging framework and configuration

Log frame classification and selection

SLF4j use

SLF4j user manual

SLF4j user manual

Use the diagram - the jar package that needs to be imported


Unified logging, even other frameworks, also use slf4j for output with me

How to make all logs in the system use slf4j

  1. Exclude other log frames in the system first
  2. Replace the original log frame with a tundish
  3. We import slf4j other implementations

SpringBoot log relationships




Log usage

1.springBoot has configured the log configuration for us by default

About using logger The incoming string in trace() became popular. The imported package does not match the imported package

Solution to the error of 'trace(java.util.function.Supplier < java.lang.String >)'... When the spring boot log uses the trace() method

The default log level of springboot is info level. If no level is specified, the default level of springboot is = = = "root level = = =" info level "

log level
From low to high: trace < debug < info < warn < error
You can adjust the log level of the output, and the log will only take effect at the higher level after this level

@RunWith(SpringRunner.class)
@SpringBootTest
class SpringBootQuickStartOneApplicationTests {
    //Recorder
    Logger logger= LoggerFactory.getLogger(getClass());
    @Test
    public void contextLoads()
    {
      //log level
        //From low to high: trace < debug < info < warn < error
        //You can adjust the log level of the output, and the log will only take effect at the higher level after this level
        logger.trace("This is trace journal...");
        logger.debug("This is debug journal");
        //springBoot uses info level by default
        logger.info("This is info level");
        logger.warn("This is warn level");
        logger.error("This is error level");
    }
}

SpringBoot log settings

  • Log level from low to high: trace, debug, info, warning, error.
  • The default log level of SpringBoot is info. The method to adjust the log level is: in application Adjust in the properties file:
  • logging.level. Package name (. Class name) = level A - > adjust the minimum log level of A package (class) to A.

To modify the default log configuration:

logging.file=springboot.log      Do not specify a path and generate a file named under the current project springboot.log Log of
logging.file=E:/springboot.log   Specify the full path in E Born under the plate springboot.log Log of
 
logging.path=/spring/log         In the root path of the current project disk (if not changed to c Create under disk spring Folder and inside log folder; use 
                                 spring.log As default file
 
Specifies the format of the log output in the console
logging.pattern.console=%d{yyyy‐MM‐dd} [%thread] %‐5level %logger{50} ‐ %msg%n
 
Specifies the format of log output in the file
logging.pattern.file=%d{yyyy‐MM‐dd} ===[%thread] ===%‐5level ===%logger{50} ====%msg%n

Log output format reference:

%d-->Represents the date and time,
%thread-->Represents the thread name,
%‐5level-->The level is displayed 5 characters wide from the left
%logger{50} -->express logger The maximum length of the name is 50 characters, otherwise it is divided according to the period.
%msg-->Log messages,%n Is a newline character
 
%d{yyyy‐MM‐dd HH:mm:ss.SSS} [%thread] %‐5level %logger{50} ‐ %msg%n

Custom log configuration

Put each logging framework's own configuration file on the classpath, and SpringBoot will not use the default configuration

If you directly use logback spring If XML is the file name, the log framework will not directly load the log configuration items, but the log configuration will be parsed by SpringBoot. You can use the advanced Profile function of SpringBoot = = = "to specify that a certain configuration will only take effect in a certain environment

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <!--You can specify that a certain configuration only takes effect in a certain environment-->
        <!--Effective production environment-->
        <springProfile name="dev">
            <pattern>%d{yyyy‐MM‐dd HH:mm:ss.SSS} ‐‐‐‐> [%thread] ‐‐‐> %‐5level%logger{50} ‐ %msg%n</pattern>
        </springProfile>
        <!--Effective in non production environment-->
        <springProfile name="!dev">
            <pattern>%d{yyyy‐MM‐dd HH:mm:ss.SSS} ==== [%thread] ==== %‐5level%logger{50} ‐ %msg%n
            </pattern>
        </springProfile>
    </layout>
</appender>

If you directly use logback XML is the file name, which will be directly recognized by the log framework. If you continue to use the profile function, you will have the following errors:

no applicable action for [springProfile]

For spring boot's unified processing of logging framework and logging settings, please refer to the following article

Spring boot unified processing of log framework and log settings

Refer to the following article for the switch log framework

Usage and switching of spring boot log framework

Added by jabapyth on Mon, 03 Jan 2022 10:29:23 +0200