编程知识 cdmana.com

Learning and organizing notes of the springboot log framework (1)

background

1、 From the original System.out.println() To print logs ;

2、 Pass the log through IO Stream is stored in the log folder ;

3、 Encapsulate a logging framework to improve the logging function ;

4、 Through encapsulation API To call the interface of the log framework ;

5、 Final form == Log framework and usage and JDBC Almost driven ;

Write a unified interface layer ; Log facade ( An abstraction layer of logs );logging-abstract.jar;
Import the specific log implementation to the project ; Our previous log framework is the abstract layer of implementation ;

The journal framework on the market

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

Log facade ( The abstraction layer of logs ) Log implementation

JCL(Jakarta Commons Logging) SLF4j(Simple Logging

Facade for Java) jboss-logging

Log4j JUL(java.util.logging)

Log4j2 Logback

 

Choose a facade on the left ( Abstraction layer )、 Choose an implementation on the right ;
Log facade : SLF4J;
Log implementation :Logback;

SpringBoot: The bottom is Spring frame ,Spring The frame defaults to JCL;‘
SpringBoot choose SLF4j and logback;
 

2、SLF4j Use

1、 How to use SLF4j https://www.slf4j.org

Later development time , Logging method call , You should not call the log implementation class directly , It calls the methods in the log abstraction layer ;
Import... Into the system slf4j Of jar and logback The implementation of the jar

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 . Use slf4j in the future , The configuration file is still the configuration file of the log implementation framework itself
Pieces of ;

2、 remaining problems

a(slf4j+logback): Spring(commons-logging)、Hibernate(jboss-logging)、MyBatis、xxxx
Unified logging , Even if other frameworks are used together with me slf4j For the output

How to unify all logs in the system to slf4j;
1、 Exclude other log frames from the system first ;
2、 Replace the original logging framework with an intermediate package ;
3、 We import slf4j


3、SpringBoot Log relationship

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

SpringBoot Use it for logging ;

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

The underlying dependency

summary :

1)、SpringBoot The bottom layer also uses slf4j+logback The way to log

2)、SpringBoot I also replaced all the other logs with slf4j;

3)、 Intermediate substitution

@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're going to introduce other frameworks ? Be sure to remove the default log dependency of this framework ?
Spring The frame 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>

SpringBoot Can automatically adapt to all the logs , And the bottom layer uses slf4j+logback The way to log , When introducing other frameworks , It only needs
Exclude the log framework that this framework depends on ;

4、 Log usage ;

1、 The default configuration

SpringBoot By default, the log is configured for us ;

// Recorder 
Logger logger = LoggerFactory.getLogger(getClass());
@Test
public void contextLoads() {
//System.out.println();
// Level of logging ;
// From low to high  trace<debug<info<warn<error
// You can adjust the log level of the output ; Logs will only take effect at this level at a later high level 
    logger.trace(" This is a trace journal ...");
    logger.debug(" This is a debug journal ...");
    //SpringBoot The default is info Grade , Use if you don't specify a level SpringBoot Default specified level ;root Level 
    logger.info(" This is a info journal ...");
    logger.warn(" This is a warn journal ...");
    logger.error(" This is a error journal ...");
}
 Log output format :
%d Means date time ,
%thread Represents the thread name ,
%‐5level: The level is shown from the left 5 Character width 
%logger{50}  Express logger The longest name 50 Characters , Otherwise, divide by periods .
%msg: Log message ,
%n Is a newline 
%d{yyyy‐MM‐dd HH:mm:ss.SSS} [%thread] %‐5level %logger{50} ‐ %msg

SpringBoot Modify the default configuration of the log

logging.level.com.atguigu=trace
#logging.path=
#  Do not specify the path to generate... Under the current project springboot.log journal 
#  You can specify the full path ;
#logging.file=G:/springboot.log
#  Create... Under the root path of the current disk spring Folder and inside log Folder ; Use  spring.log  As the default file 
logging.path=/spring/log
#  The format of the log output in the 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
logging.file logging.path Example Description
(none) (none)   Output only on the console
 
Specified file (none my.lo Output log to my.log
(none Specify the directory /var/lo Output to the specified directory spring.log writing

2、 Specify the configuration

Drop the configuration file of each log framework to the classpath ;SpringBoot It doesn't use his default configuration

Logging Syste Customizati
Logba logback-spring.xml , logback-spring.groovy , logback.xml or
logback.groovy
Log4j log4j2-spring.xml or log4j2.xml

JDK (Java Ut

Loggin

logging.properties

 

logback.xml: Directly recognized by the log framework ;
logback-spring.xml: The log framework does not directly load the log configuration items , from SpringBoot Parsing log configuration , have access to SpringBoot
Advanced Profile function

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

Such as :

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<!‐‐
     Log output format :
    %d Means date time ,
    %thread Represents the thread name ,
    %‐5level: The level is shown from the left 5 Character width 
    %logger{50}  Express logger The longest name 50 Characters , Otherwise, divide by periods .
    %msg: Log message ,
    %n Is a newline 
‐‐>
<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 you use logback.xml As log profile , And use profile function , There will be the following mistakes

no applicable action for [springProfile]

5、 Switch log frame

May, in accordance with the slf4j Log map of , Make the relevant switch ;
slf4j+log4j The way

<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 a 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>

 

版权声明
本文为[91g5cdgs]所创,转载请带上原文链接,感谢

Scroll to Top