SpringBoot used in the project Log4j2

Log4j series

  • log4j logging component is an open source implementation apache
  • logback also log4j by the author of the design is complete, with better characteristics, to replace a logging framework log4j is native to realize slf4j
  • log4j2 log4j 1.x and logback is a modified version of said new techniques employed (no lock asynchronous, etc.), so that the throughput of logs, 10 times higher than the performance log4j 1.x, and solve some deadlock bug, but more simple and flexible configuration

slf4j, the standard interface, not specific implementation of a specification of a framework for the development of all log frame, because the interface can not be used independently, needs and specific logging framework implemented in conjunction with (e.g. log4j, logback), using the interface the benefit is that when the project needs to be replaced logging framework, only need to replace jar and configuration, no changes related java code

log4j, logback, log4j2 a journaling is embodied frame, it may be used alone or in conjunction with use with slf4j

 

slf4j+log4j2

springboot projects need to import:

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

The item has introduced spring-boot-starter-web dependencies remember to remove the log-dependent spring comes spring-boot-starter-logging, 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>

 

Profiles

springboot way:
application.properties add configuration logging.config = classpath: log4j2_dev.xml, log4j2_dev.xml is log4j2 create your profile name, placed in the resources, such as on the other path, the corresponding modification

Web engineering methods:

<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");
}

 

Configuration file format: log2j configuration file can be xml format can be json format, 
the location of the configuration file: log4j2 default will look for files log4j2.xml, log4j.json, log4j.jsn other names in the classpath directory, If not found, the default configuration output, which is output to the console, also can customize the location (you need to configure in web.xml) configuration file, usually placed in the src / main / resources to the root directory 

 

Guess you like

Origin www.cnblogs.com/yxfcnbg/p/11532808.html