Using Log4j2 in SpringBoot project

Log4j series

  • log4j is an open source log component implemented by apache
  • Logback is also designed by the author of log4j. It has better features and is used to replace a logging framework of log4j. It is the native implementation of slf4j.
  • log4j2 is an improved version of log4j 1.x and logback. It is said that it adopts some new technologies (lock-free asynchronous, etc.), which makes the throughput and performance of the log 10 times higher than log4j 1.x, and solves some deadlock problems. bug, and the configuration is simpler and more flexible

slf4j is a specification, standard, and interface for all logging frameworks. It is not a specific implementation of a framework, because the interface cannot be used independently. It needs to be used in conjunction with a specific logging framework implementation (such as log4j, logback), using the interface The advantage is that when the project needs to replace the log framework, only the jar and configuration need to be replaced, and the relevant java code does not need to be changed

log4j, logback, and log4j2 are all log implementation frameworks, so they can be used alone or in combination with slf4j

 

slf4j+log4j2

The springboot project needs to import:

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

If there is an imported spring-boot-starter-web dependency package in the project, remember to remove the log dependency spring-boot-starter-logging that comes with spring, as follows:

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

 

configuration file

Springboot method:
add the configuration logging.config=classpath:log4j2_dev.xml to application.properties, log4j2_dev.xml is the name of the log4j2 configuration file you created, put it under resources, and modify it accordingly if you put it in other paths

Web engineering method:

<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>/WEB-INF/conf/log4j2.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>

纯Java方式:
public static void main(String[] args) throws IOException {
File file = new File("D:/log4j2.xml");
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
final ConfigurationSource source = new ConfigurationSource(in);
Configurator.initialize(null, source);
Logger logger = LogManager.getLogger("myLogger");
}

 

Format of configuration file: log2j configuration file can be in xml format or json format. 
Location of configuration file: log4j2 will look for files with names such as log4j2.xml, log4j.json, log4j.jsn, etc. in the classpath directory by default. If it is not found, it will be output according to the default configuration, that is, output to the console, or you can customize the location of the configuration file (which needs to be configured in web.xml), generally placed in the src/main/resources root directory. 

 

Reprinted in: https://www.cnblogs.com/yxfcnbg/p/11532808.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324838579&siteId=291194637