Log4j get started with Log4j using Log4j logging technology

Log4j logging technology

1. Concept

The log technology in the program can record the detailed process of the program when it is running, and can be stored permanently

2. The difference between log and output statements

Insert picture description here

3. The architecture of logging technology

Insert picture description here

4. Detailed explanation of Log4j configuration file

Note: The name of the Log4j configuration file must be log4j.properties. A simple example is as follows:

Insert picture description here

Three cores of the Log4j configuration file

(1) Loggers logger

i. Role: used to indicate the level of the log

ii. Format:log4j.rootLogger=日志级别,appenderName1,appenderName2...

iii. Log level:

OFF, DEBUG <INFO <WARN <ERROR <FATAL, ALL, customized level

Note: Log4j has a rule to only output log information whose level is not lower than the specified level

iv. appenderName: configure in Appenders output source

(2) Appenders output source

i. Function: output logs to different places, such as console, files, etc.

ii. Format:

Output to the console:

log4j.appender.自定义appenderName的名称=org.apache.log4j.ConsoleAppender

Output to file:

log4j.appender.自定义appenderName的名称=org.apache.log4j.FileAppender

iii. Commonly used options of ConsoleAppender:

  • ImmediateFlush=true

Indicates that all messages will be output immediately, set to false to not output, the default value is true

  • Target=System.out

Use out font color is black, use Target=System.err, font color is red

iv. Commonly used options of FileAppender:

  • ImmediateFlush=true

Indicates that all messages will be output immediately, set to false to not output, the default value is true

  • Append=false

true means adding the message to the specified file, the original message does not cover
false , the message will cover the specified file content, the default value is true

  • File=D:/logs/logging.log4j (The path address is a slash to the left)

Output log information to the specified file, this file does not need to exist, it will be created automatically

(3) Layouts

i. Function: Adjust the format of log output

ii. Format:

The layout mode can be specified flexibly:

log4j.appender.自定义appenderName的名称.layout=org.apache.log4j.PatternLayout

Contains the log information level and information string:

log4j.appender.自定义appenderName的名称.layout=org.apache.log4j.SimpleLayout

Contains information such as the time, thread, and category of the log:

log4j.appender.自定义appenderName的名称.layout=org.apache.log4j.TTCCLayout

iii. Commonly used options of PatternLayout

format:ConversionPattern=指定的格式化符号

Insert picture description here

5. Log4j use steps

(1) Import the jar package

Insert picture description here

(2) Write the log4j.properties configuration file and place this file in the src directory

log4j.rootLogger=info,my,fileAppender

### direct log messages to my ###
log4j.appender.my=org.apache.log4j.ConsoleAppender
log4j.appender.my.ImmediateFlush = true
log4j.appender.my.Target=System.out
log4j.appender.my.layout=org.apache.log4j.PatternLayout
log4j.appender.my.layout.ConversionPattern=%d %t %5p %c{1}:%L - %m%n

# fileAppender演示
log4j.appender.fileAppender=org.apache.log4j.FileAppender
log4j.appender.fileAppender.ImmediateFlush = true
log4j.appender.fileAppender.Append=true
log4j.appender.fileAppender.File=E:/log4j-log.log
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n

(3) Obtain the log object in the code and output

public class Log4JTest01 {
    
    

    //Logger类所属的包是slf4j
    //参数是当前类.class
    private static final Logger LOGGER = LoggerFactory.getLogger(Log4JTest01.class);

    public static void main(String[] args) {
    
    

        LOGGER.debug("debug级别的日志"); //没有达到配置文件中的info级别,不会输出
        LOGGER.info("info级别的日志");
        LOGGER.warn("warn级别的日志");
        LOGGER.error("error级别的日志");

    }
}

(4) Operation result

Insert picture description here

(5) Open the output file specified in the configuration file

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49343190/article/details/112004734