log: org.apache.logging.log4j log printing

simple operation

import pom

<dependencies>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.16.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.15.0</version>
    </dependency>
</dependencies>

resources resource introduction

Create a new log4j2.xml file (  log4j.properties  can also see personal habits)

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

Detailed configuration:  Log4j2 Chinese Documentation - Log4j2 2.x Manual | Docs4dev

create object

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LogTextMain {
    private static final Logger log = LogManager.getLogger(LogTextMain.class);
    public static void main(String[] args) {
        log.info("程序开始=====");
    }
}

log level

There are 8 levels in total, from low to high: All < Trace < Debug < Info < Warn < Error < Fatal < OFF.

All: The lowest level, used to enable all logging.

Trace: It is tracking, that is, after the program advances, you can write a trace output, so there should be a lot of traces, but it doesn't matter, we can set the minimum log level to prevent him from outputting.

Debug: It is very helpful to point out fine-grained information events for debugging applications. ( General use )

Info: The message highlights the running process of the application at a coarse-grained level. ( General use )

Warn: Output warnings and logs below warn.

Error: Output error message log.

Fatal: Outputs a log of each fatal error event that will cause the application to exit.

OFF: The highest level, used to turn off all logging.

The program will print logs higher than or equal to the set level. The higher the set log level, the fewer logs will be printed.

Guess you like

Origin blog.csdn.net/Aoutlaw/article/details/130983166
log
log