Java study notes-the use of @Slf4j log

这篇文章写的很好:https://blog.csdn.net/u011781521/article/details/55002553

@Slf4j cannot be used alone, but can be used in combination with log4j or logback. The following will describe how to use it separately

The premise of @Slf4j is: install the lombok plugin and introduce dependencies

SLF4J is different from other log libraries and is very different from other log libraries. SLF4J (Simple logging Facade for Java) is not a real logging implementation, but an abstraction layer, which allows you to use any logging library in the background.

 

One.@Slf4j+log4j

1. Introduce dependency log4j in pom

 <dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.17</version>
</dependency>

 <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>1.7.25</version>
 </dependency>

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

Add a log4j.properties file under the resource folder and configure it as follows:


log4j.rootLogger = debug,stdout,D,E

### 输出信息到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出DEBUG 级别以上的日志到= ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = /Users/xxx/Desktop/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志到= ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =/Users/xxx/Desktop/logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n 

 

2. Test the code

import  org.apache.log4j.Logger;

public class Test {
    private static Logger logger = Logger.getLogger(Test.class);

    /**
     * @param args
     */
    public static void main(String[] args) {
        // System.out.println("This is println message.");

        // 记录debug级别的信息
        logger.debug("This is debug message.");
        // 记录info级别的信息
        logger.info("This is info message.");
        // 记录error级别的信息
        logger.error("This is error message.");
    }

}

 

The input log is as follows:

 

2.@Slf4j+logback (recommended)

@Slf4j Same as before, install plugins and dependencies

  <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
  </dependency>

 

Three. Detailed configuration file

Log4j is composed of three important components: Loggers, Appenders and Layout.

  • Logger: Control which logging statements to be enabled or disabled, and limit the level of log information
  • Appenders: Specifies whether the log will be printed to the console or to a file
  • Layout: Control the display format of log information

 The Log information to be output in Log4j defines 5 levels, which are DEBUG, INFO, WARN, ERROR, and FATAL. When outputting, only information with a level higher than the level specified in the configuration can be truly output, which is very convenient It is really convenient to configure the content to be output in different situations without changing the code.

1. Configure the root Logger, the syntax is:

log4j.rootLogger = [ level ] , appenderName, appenderName, …

2. Output directly to the console, then log.info directly in the code

The main configuration file refers to the link at the beginning of the article

 

 

 

Guess you like

Origin blog.csdn.net/mumuwang1234/article/details/110652944