[SpringBoot] Integrated Logging Framework

1. Overview of log framework

Generation of 1.1 Log Framework

  • Imagine that you are a project experience with a large system in a company and you want to record some information about how the system works:
    • The first thought was to use System.out.println("") to print key data to the console, but that was too much of a hassle.
    • So you want to input its log information into the file, and then you develop a framework to record some runtime information of the system, which is the original log framework, zhanglogging.jar;
    • Later, you want to add several advanced features (asynchronous mode, automatic archiving, etc.) to zhanglogging-good.jar, so the second-generation framework is developed, but because the system uses the first-generation log framework, it will take a lot of work to remove the previous framework and replace it with a new one.
    • For the flexibility of the framework in the future, you re-modify the previous API; at this point, you think of the JDBC database-driven design ideas, and design the zhanglogging-prefect.jar;

1.2 Market Logging Framework

  • JUL,JCL,Jboss-logging,logback,log4j,log4j2,slf4j....

  • Select a facet on the left (abstraction layer) and an implementation on the right.
    • Log Face: SLF4J;
    • Log implementation: Logback;
  • SpringBoot: The bottom level is the Spring framework, which is JCL by default;
  • SpringBoot uses SLF4j and logback;

2. Use and Integration of SLF4j

2.1 How to use SLF4j in the system

Official address

  • In future development, calls to logging methods should not directly call the log implementation class, but instead call the methods in the log abstraction layer; import the jar of slf4j and the implementation jar of logback into the system

  • Each log implementation framework has its own configuration file.After using slf4j, the configuration file is still a log implementation framework's own configuration file;

2.2 How to integrate the log framework

  • The log framework slf4j+logback is used by default in SpringBoot, but commons-logging is used by default in Spring, and jboss-logging is used by default in Hibernate.
  • How do I unify logging in SpringBoot, even if other frameworks work with me to unify slf4j output?
  • As you can see from the diagram below, you can replace the original log framework with its corresponding sf4j log conversion package. picture source

  • How to unify all logs in the system to slf4j;
  • 1. Exclude other log frameworks from the system first;
  • 2. Replace the original log framework with the intermediate package;
  • 3. We import other implementations of slf4j

2.3 Logging relationships in SpringBoot


Underlying dependencies


  • 1), SpringBoot underlying is also logging using slf4j+logback
  • 2) SpringBoot also replaced the other logs with slf4j;
  • 3) Intermediate replacement package?
  • 4) If we want to introduce another framework, do remove the default log dependencies for this framework? The Spring framework uses commons-logging;
  • SpringBoot automatically adapts all logs, and the underlying uses slf4j+logback to log. When introducing other frameworks, you just need to exclude the log framework that this framework depends on.

3. Log Use

3.1 Default Configuration

  • SpringBoot helps us configure our logs by default;
Logger logger = LoggerFactory.getLogger(getClass());
    public void logTest1() {
        //Level of log;
        //From low to high trace<debug<info<warn<error
        //You can adjust the log level of the output; the log will only take effect at this level and at a later level
        logger.trace("This is trace Journal...");
        logger.debug("This is debug Journal...");
        //SpringBoot defaults to info level for us, SpringBoot default level for no specified level; root level
        logger.info("This is info Journal...");
        logger.warn("This is warn Journal...");
        logger.error("This is error Journal...");

3.2 Log Format

Log output format symbol meaning:

%d represents the date and time,
%threads denotes the thread name,
%- 5level: Level shows 5 character width from left
 %logger{50} means that the logger name has a maximum of 50 characters, otherwise it is split by a period.%msg: Log message,
%n is a line break
%d{yyyy‐MM‐dd HH:mm:ss.SSS} [%thread] %‐5level %logger{50} ‐ %msg%n

SpringBoot Modifies the Default Configuration of Logs

# Generate the springboot.log log under the current project without specifying a path
# You can specify a complete path;
# Use spring.log as the default file
# You can also create the spring folder and the log folder inside the current disk's root path.

#  Format of log output in console
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
# Specify the format of log output in the file
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n

3.2 Specify Configuration

  • Put each logging framework's own configuration file under the class path; SpringBoot does not use his default configuration

  • logback.xml: directly identified by the log framework;
  • logback-spring.xml: The log framework does not directly load the log's configuration items, and is configured by SpringBoot to parse the log, using SpringBoot's advanced Profile functionality.
  • For example, you can specify that a configuration only works in a certain environment
 <layout class="ch.qos.logback.classic.PatternLayout">
            <springProfile name="dev">
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ----> [%thread] ---> %-5level %logger{50} - %msg%n</pattern>
            <springProfile name="!dev">
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ==== [%thread] ==== %-5level %logger{50} - %msg%n</pattern>
  • If logback.xml is used as the name of the log configuration file and profile is also used, the following errors will occur
  • no applicable action for [springProfile]

4. Switch Log Framework

The log adapter of slf4j can be used for related switching.
The way slf4j+log4j;


Switch to log4j2


Tags: Java SpringBoot Spring log4j xml

Posted on Fri, 20 Sep 2019 10:43:31 -0700 by g5604