This is the 24th day of my participation in the August More Text Challenge

In the process of developing The SpringBoot project, in order to better record the operation information of the project, as well as locate and track problems, we will adopt the way of logging. SpringBoot uses the Jakarta Commons Logging API (JCL) internally, and the default configuration provides support for commonly used Logging, which can be configured to output log content using a console or file.

SLF4J

The definition of SLF4J

SLF4J, which stands For Simple Logging Facade For Java, is a unified Facade abstraction For various Java Logging frameworks in SpringBoot. It defines a unified Logging abstraction interface, and the real Logging implementation is determined at run time. In practice, each logging framework is developed for this interface. SLF4J provides a unified API interface for all logging, but the implementation is different. This pattern is called facade pattern. SpringBoot implements the LogBack logging framework by default.

SLF4J usage specification

  • In alibaba development manual mandatory requirements, in the project application can not directly use log4J, Logback API, but should rely on the LOGGING framework SLF4J API, using the facade mode of the log framework.
  • The use of uniform standard SLF4J interface is conducive to maintaining the unity of log processing methods in different logging frameworks of each class and module.

SLF4J supports a logging framework

From the SLF4J support framework structure diagram provided on the official website, it can be seen that logback, log4J and other logging frameworks can use the unified interface of SLF4J in the application by converting certain Jar packages. In this way, no matter what framework is used, only the relevant JARS need to be introduced, and the logic used does not need to be changed.

SLF4J is developed and used in applications

SLF4J exists in the SpringBoot boot class and is automatically loaded.

In practice, just create a Logger object and use it.

Logger logger = LoggerFactory.getLogger(ControllerTestSLF4J.class);
logger.info("logger.info : hello!");
Copy the code

SLF4J adaptation of the logging framework

  • The Log4j framework requires an adaptation layer to use SLF4J as a facade, and log4j provides slF4J-api.jar and SLF-4j-log412.jar dependencies.
  • Slf4j-api.jar is used as an adaptation, and the SLf4J-api.jar API is actually called in slf4j-api.jar, which executes the specific methods in log4j.
  • The same is true for other logging frameworks. With the adaptation layer, many logging frameworks can now use the SLF4J API for logging operations.

Log configuration in SpringBoot

Configure logs in the configuration file

  • The logging properties can be configured in the Application. properties configuration file of the SpringBoot project
  • logging.level.*: Controls log levels,*The value can be TRACE<DEBUG<INFO<ERROR<TATAL. After the value is set, information lower than the current level will not be displayed.
  • logging.pattern: Customizes output formats in console and file
  • Logs are output to files
    • SpringBoot prints logs to the console by default, and you can set the logging.file or logging.path property in the configuration file to print logs to a file
    • Logging.file: Sets the file name, either absolute or relative, such as =my.log
    • Logging. path: Sets the directory, if log files will be created in this directory
    • If only the file attribute is set, the specified log file is created in the current path of the project. If only the path property is set, spring.log is created in the specified directory

Use commands to set log levels

You can use the java-jar springtest. Jar –debug command when running the Jar package to set the log output level for the current project.

Use a separate log configuration file

  • The default naming rules for external configuration files are as follows:

  • SpringBoot officially recommends naming SpringBoot with -sprring. In this case, SpringBoot can automatically add some configuration items. By default, the configuration file is stored in the SRC /main/resources directory.
  • You can also customize the profile name, but specify the specific profile in the SpringBoot profile: logging.config= CLASSPath :logging.xml

Specific use of logs

  • If you use the Lombok plug-in, you can use the @slf4j annotation to output logs directly with log.error.
  • If you are not using the Lombok plug-in, you need to create a Logger object,Private static final Logger log = loggerFactory.getLogger (xxx.class);Create a static object for each class to reduce resource waste.
  • Use [{}] to isolate parameter variables. Do not use string concatenation. Many string objects will be generated, occupying space and affecting performance\
  • Check whether the debug log level is dubug before using it\

The last

A brief record of the SLF4J log facade in the project, in the subsequent business will continue to learn and improve.