Source: Internet
Author: User

Log4j2 related introduction Can Baidu look, here only pay attention to configure LOG4J2 can immediately run up;

1. Add LOG4J2 related MAVEN configuration information to the Pom.xml file

<!--log4j2 -        <Dependency>            <groupId>org.apache.logging.log4j</groupId>            <Artifactid>Log4j-core</Artifactid>            <version>2.1</version>        </Dependency>        <Dependency>            <groupId>org.apache.logging.log4j</groupId>            <Artifactid>Log4j-api</Artifactid>            <version>2.1</version>        </Dependency>        <Dependency>            <groupId>org.apache.logging.log4j</groupId>            <Artifactid>Log4j-web</Artifactid>            <version>2.1</version>        </Dependency>

2. Add LOG4J2 configuration information to the Web. xml file

<!--log4j -    <Context-param>          <Param-name>Islog4jautoinitializationdisabled</Param-name>          <Param-value>False</Param-value>      </Context-param>      <!--This file is found by default and does not require additional configuration references <context-param> <param-name>log4jConfiguration</param-name> <param-value>classpath:/log4j2.xml</param-value> </context-param> -    <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>      </filter-mapping>

3, add Log4j2.xml file, specific configuration refer to the following file contents

<?XML version= "1.0" encoding= "UTF-8"?><!--log4j2 Usage instructions (create by Seanxiao): Use the following: private static final Logger Logger = Logmanager.getlogger (actual class name. Class.getnam E ()); 2, log Description: (1) Please configure the parameters according to the actual situation (2) need to pay attention to the log file backup number and log file size, note the reserved directory space (3) The actual deployment of the Backupfilepatch variable needs to be modified to the Linux directory -<ConfigurationStatus= "Error">    <Properties>        < Propertyname= "FileName">Front.log</ Property>        < Propertyname= "Backupfilepatch">d:/usr/front/log/</ Property>      </Properties>    <!--define all the Appender first -    <appenders>        <!--The configuration of this output console -        <Consolename= "Console"Target= "System_out">             <!--console only outputs level and above information (Onmatch), other direct rejections (Onmismatch) -            <Thresholdfilter Level= "Trace"Onmatch= "ACCEPT"Onmismatch= "DENY" />            <!--This is all about the format of the output log. -            <Patternlayoutpattern= "%d{hh:mm:ss." SSS}%-5level%class{36}%l%M-%msg%xex%n " />        </Console>                <!--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= "${backupfilepatch}${filename}"Filepattern= "${backupfilepatch}$${date:yyyy-mm}/app-%d{yyyymmddhhmmsssss}.log.gz">            <Patternlayoutpattern= "%d{yyyy." Mm.dd ' at ' HH:mm:ss. SSS Z}%-5level%class{36}%l%M-%msg%xex%n " />                        <!--log File Size -            <Sizebasedtriggeringpolicysize= "20MB" />            <!--Maximum number of files reserved -            <DefaultrolloverstrategyMax= " the"/>        </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 -        <Loggername= "Com.lhyl.front" Level= "Trace"additivity= "true">            <Appenderrefref= "Rollingfile" />        </Logger>        <Root Level= "Error">            <Appenderrefref= "Console" />        </Root>    </Loggers></Configuration>

4, using the same method as log4j, in the use of the class to add the following object initialization, and then call Logger.error\debug at the critical location, and so on ...

Private Static Final Logger Logger = Logmanager.getlogger (actual class name.  Class. GetName ());

Scattered reference information online .....


Related Article

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.