log4j Details (ii) How XML is configured

Source: Internet
Author: User

The previous article on the log4j of the properties of the configuration, although the sense of difference is not very large, but the Log4j.properties configuration method can not use filter filter to limit the log level, another reason is Log4j.xml SQL statements can be placed in the Conversionpattern parameter of the Layout property when the database is inserted, and the log4j.properties configuration becomes more cumbersome .

First, step

1. Introduction of the JAR package

Create a log4j.xml file under 2.SRC

3. Configuration header (important)

4. Configure XML

Second, the code part

XML file

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" ><log4j:configuration xmlns:log4j= ' http://jakarta.apache.org/ log4j/' ><!--The following is the definition of Appender, which defines the destination, output mode, and filtering level of the log output--><!--* 1. A appender child element defines a log output destination * 2. A logger child element defines a log writer--><appender name= "myfileappend" class= "Org.apache.log4j.RollingFileAppender" >< param name= "file" value= "E:logs//output.log"/><!--set the log output file name--><!--set whether to add a new log on the base of the original log when the service is restarted-- <param name= "Append" value= "true"/><param name= "Maxbackupindex" value= "ten"/><layout class= " Org.apache.log4j.PatternLayout "><param name=" Conversionpattern "value="%p (%c:%l)-%m%n "/></layout> <!--Levelrangefilter: is a very simple filter based on horizontal matching and can reject message priority within a certain range. Filter admits three options levelmin, Levelmax, Acceptonmatch. If the level of loggingevent is not Min and Max (containment), then filter. Denied return. If the log event level is within the specified range, then if Acceptonmatch is true, the filter. Accept the return if Acceptonmatch is false, filter. Neutral return. If levelmin is not defined, then there is no minimum acceptable level (that is, the level never refuses to be too "low"/Not important). If the Levelmax is not defined, then there is no maximum acceptable level (that is, the level will not reject the "high"/important). The reference Setthreshold method can be used for all output source extensions Appenderskeleton to filter the event level in a more convenient way. --><filter class= "Org.apache.log4j.varia.LevelRangeFilter" ><param name= "Levelmin" value= "DEBUG" > </param><param name= "Levelmax" value= "DEBUG" ></param><param name= "Acceptonmatch" value= "true" ></param></filter></appender><!--Console output--><appender name= "Consoleappend" class= " Org.apache.log4j.ConsoleAppender "><layout class=" org.apache.log4j.PatternLayout "><param name=" Conversionpattern "value="%-d{yyyy-mm-dd HH:mm:ss} [%5p][%t][%c{1}]-[%m]%m%n "/></layout></appender ><appender name= "Activexappender" class= "Org.apache.log4j.DailyRollingFileAppender" ><param name= "File "Value=" E:/logs/activex.log "/><param name=" Appender "value=" false "></param><param name=" Datepattern "value=" '. ' Yyyy-mm-dd '. Log ' "/><layout class=" Org.apache.log4j.PatternLayout "><paraM name= "Conversionpattern" value= "[%d{mmdd HH:mm:ss sss\}%-5p] [%t]%c{3\}-%m%n"/></layout></appender ><!--log Writer: Each logger can have multiple output destinations and output modes additivity indicates whether to follow the default inheritance mechanism--><logger name= " Com.inspur.log4j.testLog4j "additivity=" false "><level value=" DEBUG "></level><appender-ref ref=" Consoleappend "></appender-ref></logger><!--root logger settings--><root><priority value=" Debug "/><appender-ref ref=" myfileappend "/><appender-ref ref=" Activexappender "/></root></ Log4j:configuration>

Test class

Package Com.inspur.log4j;import Org.apache.log4j.logger;public class testlog4j {public static final Logger log = Logger.g Etlogger (testlog4j.class);p ublic static void Main (string[] args) {System.out.println ("Start."); for (int i = 0; i < 4; i++) {Log.info ("Enter the main () ...)"), Log.debug ("Enter the main () ...."); Log.warn ("Enter the MAI N () ...); Log.error ("exception!"); System.out.println ("This is log4j test."); Log.info ("End.");}}
Remember to create a logs folder under the specified directory ~


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

log4j Details (ii) How XML is configured

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.