Using LOG4J2 to log logs under Java

Source: Internet
Author: User

1. Definition:

Log4j2 refers to log4j 2.X and later

2. Installation

Log4j-core-xx.jar
Log4j-api-xx.jar
Log4j-web-xx.jar (required reference for Web project)

3. Configuration

The configuration file location is located in: src root directory, even if there is no configuration file, it will not error, the default is the form of console output.

Log4j2 configuration file with Log4 (1. The x version is very different) and can only be in. xml,. json, or. jsn format, which is configured as follows (${web:rootdir} represents the Web root directory):

1 <?XML version= "1.0" encoding= "UTF-8"?>2 3 <ConfigurationStatus= "Error">4     <!--define all the Appender first -5     <appenders>6         <!--The configuration of this output console -7         <Consolename= "Console"Target= "System_out">8             <!--This is the format of the output log -9             <Patternlayoutpattern= "%d{hh:mm:ss." SSS}%-5level%class{36}%l%M-%msg%xex%n "/>Ten         </Console> One         <!--The file will print out all the information, and this log will be automatically emptied each time the program is run, determined by the Append property, suitable for temporary testing - A         <Filename= "Error"FileName= "${web:rootdir}/logs/error.log"Append= "false"> -             <!--filerecord level and above information only (Onmatch), other direct rejection (Onmismatch) - -             <Thresholdfilter Level= "Error"Onmatch= "ACCEPT"Onmismatch= "DENY"/> the             <Patternlayoutpattern= "%d{hh:mm:ss." SSS}%-5level%class{36}%l%M-%msg%xex%n "/> -         </File> -  -         <!--This will print out all the information, each time the size exceeds size, the size of the log will be automatically saved by the year-month folder created under and compressed, as the archive - +         <Rollingfilename= "Rollingfile"FileName= "${web:rootdir}/logs/history.log" - Filepattern= "log/$${date:yyyy-mm}/history-%d{mm-dd-yyyy}-%i.log.gz"> +             <Patternlayoutpattern= "%d{yyyy-mm-dd ' at ' HH:mm:ss z}%-5level%class{36}%l%M-%msg%xex%n"/> A             <Sizebasedtriggeringpolicysize= "50MB"/> at         </Rollingfile> -     </appenders> -     <!--then define the logger, only the appender,appender that define the logger and introduce will take effect - -     <Loggers> -         <!--Create a default root logger - -         <Root Level= "Trace"> in             <Appender-refref= "Error"/> -             <Appender-refref= "Rollingfile"/> to             <Appender-refref= "Console"/> +         </Root> -     </Loggers> the </Configuration>

4. General projects and Web projects

For ordinary projects, the above configuration is done to normal use, for Web projects, is not generated log files. You need to add the following configuration in the Web. XML <web-app> root node:

1 <!--log4j2.x Start -2 <Listener>3     <Listener-class>Org.apache.logging.log4j.web.Log4jServletContextListener</Listener-class>4 </Listener>5 <Filter>6     <Filter-name>Log4jservletfilter</Filter-name>7     <Filter-class>Org.apache.logging.log4j.web.Log4jServletFilter</Filter-class>8 </Filter>9 <filter-mapping>Ten     <Filter-name>Log4jservletfilter</Filter-name> One     <Url-pattern>/*</Url-pattern> A </filter-mapping> - <!--log4j2.x End -

(myeclipse example) click here to download

Using LOG4J2 to log logs under Java

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.