Log4j2 of the use of the detailed (1) _log4j/log4j2

Source: Internet
Author: User
Here the SLF4J and LOG4J2 integration approach is used.
One, required jar packs or dependencies: SLF4J-API
Log4j-api Log4j-core Log4j-slf4j-impl (Integrated package) Log4j-web (Web project required) Note: The latest log4j2 requires a high JDK version, Because of the jdk1.6 used here, the maximum version can only use 2.3, the specific version used here is as follows: Slf4j-api-1.7.25.jar
Log4j-api-2.3.jar Log4j-core-2.3.jar Log4j-slf4j-impl-2.3.jar Log4j-web-2.3.jar
Second, the log4j configuration file: The log4j version of the previous 1.x support for the properties configuration file, but log4j2 here, only support the way XML configuration files, or json,jsn suffix files. The priority of the System selection profile (from) is as follows: (1). classpath a file named Log4j2-test.json or LOG4J2-TEST.JSN. (2). A file named Log4j2-test.xml under Classpath. (3). classpath a file named Log4j2.json or LOG4J2.JSN. (4). classpath the file named Log4j2.xml. We typically use Log4j2.xml to name them by default. If you want to test locally, you can put Log4j2-test.xml into the classpath, and the formal environment uses log4j2.xml, do not pack log4j2-test.xml when you package the deployment. The default configuration file for Log4j2.xml profile is:
<?xml version= "1.0" encoding= "UTF-8"?> <configuration status= "WARN" > <Appenders> <console name= " Console "target=" System_out "> <patternlayout pattern="%d{hh:mm:ss. SSS} [%t]%-5level%logger{36}-%msg%n "/> </Console> </Appenders> <Loggers> <root level=" Error " > <appenderref ref= "Console"/> </Root> </Loggers> </Configuration> Below is a more detailed configuration:
<?xml version= "1.0" encoding= "UTF-8"?>
<configuration status= "INFO" monitorinterval= "ten" >
<Properties>
<!--configuration log file output directory-->
<property name= "Log_dir" >/export/data/logs/myProject_web</Property>
</Properties>
<Appenders>
<console name= "Console" target= "System_out" >
<patternlayout pattern= "[%d{yyyy-mm-dd HH:mm:ss}] [%5p]%l-->%m"%n "/>
</Console>
<rollingfile name= "Debug_logger_appender" filename= "${log_dir}/web_debug.log"
Filepattern= "${log_dir}/web_debug.log.%d{yyyy-mm-dd}" >
<!--the console outputs only level and above levels of information (Onmatch), and other direct rejections (Onmismatch)-->
<thresholdfilter level= "DEBUG" onmatch= "ACCEPT" onmismatch= "DENY"/>
<patternlayout pattern= "[%d{yyyy-mm-dd HH:mm:ss}] [%5p]%l-->%m"%n "/>
<Policies>
<timebasedtriggeringpolicy modulate= "true" interval= "1"/>
</Policies>
<defaultrolloverstrategy max= "Ten"/>
</RollingFile>
<rollingfile name= "Info_logger_appender" filename= "${log_dir}/web_info.log"
Filepattern= "${log_dir}/web_info.log.%d{yyyy-mm-dd}" >
<!--the console outputs only level and above levels of information (Onmatch), and other direct rejections (Onmismatch)-->
<thresholdfilter level= "INFO" onmatch= "ACCEPT" onmismatch= "DENY"/>
<patternlayout pattern= "[%d{yyyy-mm-dd HH:mm:ss}] [%5p]%l-->%m"%n "/>
<Policies>
<timebasedtriggeringpolicy modulate= "true" interval= "1"/>
</Policies>
<defaultrolloverstrategy max= "Ten"/>
</RollingFile>
<rollingfile name= "Error_logger_appender" filename= "${log_dir}/web_error.log"
Filepattern= "${log_dir}/web_error.log.%d{yyyy-mm-dd}" >
<!--the console outputs only level and above levels of information (Onmatch), and other direct rejections (Onmismatch)-->
<thresholdfilter level= "ERROR" onmatch= "ACCEPT" onmismatch= "DENY"/>
<patternlayout pattern= "[%d{yyyy-mm-dd HH:mm:ss}] [%5p]%l-->%m"%n "/>
<Policies>
<timebasedtriggeringpolicy modulate= "true" interval= "1"/>
</Policies>
<defaultrolloverstrategy max= "Ten"/>
</RollingFile>
</Appenders>
<Loggers>
<root level= "INFO" >
<appenderref ref= "Console"/>
<!--<appenderref ref= "Debug_logger_appender"/>-->
<appenderref ref= "Info_logger_appender"/>
<appenderref ref= "Error_logger_appender"/>
</Root>
<!--filter out some of the useless debug information for spring-->
<logger name= "Org.springframework" level= "ERROR"/>
</Loggers>
</Configuration>

Three, how to use Log4j2 in Java code
Log4j2 Way
private static final Org.apache.logging.log4j.Logger Logger = Org.apache.logging.log4j.LogManager.getLogger ( Test.class);

SLF4J Way
private static final Org.slf4j.Logger Logger = Org.slf4j.LoggerFactory.getLogger (Test.class);
The point to be made here is to use the. class to generate the Logger, in the configuration file, with the full path of the package name or class name, configure the Logger tag corresponds to, for example: <logger name= "Org.springframework" level= "ERROR"/>. If the argument is a string, such as Org.slf4j.LoggerFactory.getLogger ("test"), the corresponding Logger label is configured to <logger name= "test" level= "ERROR"/>.
Four, web project use LOG4J2 note Reference website and various data show that the Web project needs to use Log4j-web dependencies, and servlet3.0 and servlet2.5 processing is different, do not support servlet2.4 and the following version. The version of the servlet is used and is bound by the Tomcat version. The following is a table of servlet and JSP versions that are supported by each version.

Servlet Spec JSP Spec EL spec WebSocket spec Apache Tomcat version Actual release revision Support Java versions 4.0 TBD ( 2.4?) TBD (3.1?) TBD (1.2?) 9.0.x None 8 and later 3.1 2.3 3.0 1.1 8.0.x 8.0.15 7 and later 3.0 2.2 2.2 1.1 7.0.x 7.0.57 6 and later
(WebSocket 1.1 requires 7 or later) 2.5 2.1 2.1 N/a 6.0.x 6.0.43 5 and later 2.4 2.0 n/a N/a 5.5.x (archived) 5.5.36 (arch ived) 1.4 and later 2.3 1.2/n/a 4.1.x (archived) 4.1.40 (archived) 1.3 and later 2.2 1.1/N/a 3.3.x (archived) 3.3. 2 (archived) 1.1 and later
The tomcat6.0.33 version used here is supported by the servlet version 2.5. (Find Tomcat under the Lib Jsp-api.jar and Servlet-api.jar two jar packages, respectively, with WinRAR this kind of decompression software open, inside the Meta-inf folder MANIFEST.MF file, which has the corresponding version number) The official website gives, if is servlet2.5, then after adding Log4j-web's dependency, needs to carry on the following configuration in the Web.xml, and needs to note, all must place before the other definition:
<listener> <listener-class>org.apache.logging.log4j.web.log4jservletcontextlistener</ listener-class> </listener>   <filter> <filter-name>log4jservletfilter</filter-name > <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class> </filter> <filter-mapping> <filter-name>log4jServletFilter</filter-name> <url-pattern>/*</ Url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> < Dispatcher>include</dispatcher> <dispatcher>ERROR</dispatcher> <dispatcher>async</ dispatcher><!--Servlet 3.0 w/disabled auto-initialization only; Not supported in 2.5--> </filter-mapping> but test here, did not carry on the above configuration, also did not add log4j-web dependence, also can use normally, print the log, is the performance above, The open shutdown of the file resource has an effect. Here reference to the article: https://logging.apache.org/log4j/2.x/manual/webapp.html http://blog.csdn.net/sushengmiyan/article/ details/52390211 http://www.cnblogs.com/yoyotl/p/4920073.html In addition, the servlet Web.xml header information for different versions (2.3,2.4,2.5,3.0) can be: http://blog.csdn.net/z69183787/article/details/36008097

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.