Spring boot core log

1. Log framework

Xiao Zhang; develop a large system;

​ 1, System.out.println(""); print key data on the console; remove the "key data" button? Write in a file?

2. Framework to record some runtime information of the system; log framework; zhanglogging.jar ;

3. How many functions are there on the high? Asynchronous mode? Auto archive? xxxx? zhanglogging-good.jar ?

4. Remove the previous frame? Replace with a new framework and modify the previous related API- prefect.jar ;

5. JDBC - database driver;

Write a unified interface layer; log facade (an abstract layer of log); logging-abstract.jar ;

Just import the specific log implementation into the project; our previous log framework is the abstract layer of the implementation;

Market log framework;

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

Log facade (abstract layer of log) Log implementation
JCL(Jakarta Commons Logging) SLF4j(Simple Logging Facade for Java) jboss-logging Log4j JUL(java.util.logging) Log4j2 Logback

Select a facade (abstract layer) on the left and an implementation on the right;

Log face: SLF4J;

Log implementation: Logback;

Spring boot: the bottom layer is the spring framework, which uses JCL by default‘

SLF4j and logback are selected for SpringBoot;

2. SLF4j use

1. How to use slf4j in the system https://www.slf4j.org

In future development, the call of logging method should not directly call the log implementation class, but the method in the log abstraction layer;

Import slf4j jar and logback implementation jar into the system

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

Each log implementation framework has its own configuration file. After using slf4j, the configuration file is still the configuration file of the log implementation framework itself;

2. Remaining problems

a(slf4j+logback): Spring(commons-logging),Hibernate(jboss-logging),MyBatis,xxxx

Unified logging, even if other frameworks use slf4j for output together with me?

How to unify all logs in the system to slf4j;

1. Exclude other log frames from the system first;

2. The original log framework is replaced by the tundish;

3. We import other implementations of slf4j

3, SpringBoot log relationship

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

Spring boot uses it for logging;

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</dependency>

Summary:

1) the bottom layer of spring boot also uses slf4j+logback for logging

2) SpringBoot also replaces other logs with slf4j;

3) intermediate replacement package: org.slf4j:jcl-over-slf4j

Source code:

@SuppressWarnings("rawtypes")
public abstract class LogFactory {

    static String UNSUPPORTED_OPERATION_IN_JCL_OVER_SLF4J = "http://www.slf4j.org/codes.html#unsupported_operation_in_jcl_over_slf4j";

    static LogFactory logFactory = new SLF4JLogFactory();

4) if we want to introduce other frameworks? Do you want to remove the default log dependency of this framework?

Spring framework uses commons logging;

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

Spring boot can automatically adapt all logs, and the bottom layer uses slf4j+logback to record logs. When other frameworks are introduced, only the log framework that this framework relies on can be excluded;

4. Log usage;

1. Default configuration

Spring boot helps us configure logs by default;

	//Recorder
	Logger logger = LoggerFactory.getLogger(getClass());
	@Test
	public void contextLoads() {
		//System.out.println();

		//Log level;
		//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 at a later high level
		logger.trace("This is trace journal...");
		logger.debug("This is debug journal...");
		//By default, SpringBoot uses the info level for us. If no level is specified, it uses the level specified by SpringBoot by default; root level
		logger.info("This is info journal...");
		logger.warn("This is warn journal...");
		logger.error("This is error journal...");


	}
    Log output format:
		%d is the date and time,
		%Thread indicates the thread name,
		%-5level: level displays 5 characters width from left
		%logger{50} indicates that the longest name of a logger is 50 characters, otherwise, it is separated by periods. 
		%msg: log message,
		%n is line break
    -->
    %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n

Spring boot modifies the default configuration of logs

logging.level.com.atguigu=trace


#logging.path=
# Do not specify path to build under current project springboot.log journal
# The complete path can be specified;
#logging.file=G:/springboot.log

# Create the spring folder and the log folder in the root path of the current disk; use the spring.log  As default file
logging.path=/spring/log

#  Format of log output in the console
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
# Specifies the format of the log output in the file
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n
logging.file logging.path Example Description
(none) (none) Console output only
specify a filename (none) my.log Output log to my.log file
(none) Specify directory /var/log Output to the specified directory spring.log In file

2. Specify configuration

Just drop each log framework's own configuration file to the classpath; spring boot doesn't use its default configuration

Logging System Customization
Logback logback-spring.xml, logback-spring.groovy, logback.xml or logback.groovy
Log4j2 log4j2-spring.xml or log4j2.xml
JDK (Java Util Logging) logging.properties

logback.xml : directly recognized by the log framework;

logback-spring.xml : the log framework does not directly load the configuration items of the log. The spring boot parses the log configuration. You can use the advanced Profile function of spring boot

<springProfile name="staging">
    <!-- configuration to be enabled when the "staging" profile is active -->
  	You can specify that a certain configuration will only take effect in a certain environment
</springProfile>

For example:

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <!--
        Log output format:
			%d is the date and time,
			%Thread indicates the thread name,
			%-5level: level displays 5 characters width from left
			%logger{50} indicates that the logger name can be up to 50 characters long, otherwise, it is separated by periods. 
			%msg: log message,
			%n is line break
        -->
        <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>
            <springProfile name="!dev">
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ==== [%thread] ==== %-5level %logger{50} - %msg%n</pattern>
            </springProfile>
        </layout>
    </appender>

If using logback.xml As a log configuration file, but also using the profile function, there will be the following errors

no applicable action for [springProfile]

5. Switch log frame

According to the log adaptation diagram of slf4j, the relevant switching can be performed;

slf4j+log4j;

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <artifactId>logback-classic</artifactId>
      <groupId>ch.qos.logback</groupId>
    </exclusion>
    <exclusion>
      <artifactId>log4j-over-slf4j</artifactId>
      <groupId>org.slf4j</groupId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
</dependency>

Switch to log4j2

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

Tags: Spring SpringBoot xml log4j

Posted on Sun, 14 Jun 2020 00:41:48 -0400 by jobs