Configuration information of Log4j. xml and Log4j. xml

Source: Internet
Author: User

Configuration information of Log4j. xml and Log4j. xml

Source code

 1 <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> 2  3     <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> 4         <layout class="org.apache.log4j.PatternLayout"> 5             <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %c%n -- %p -- %m%n" /> 6         </layout> 7     </appender> 8  9     <appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">10         <param name="File" value="${report.root}/logs/cdjwPowerPub-debug.log" />11         <param name="Append" value="true" />12         <param name="MaxFileSize" value="1024KB" />13         <param name="MaxBackupIndex" value="5" />14         <layout class="org.apache.log4j.PatternLayout">15             <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %c%n -- %p -- %m%n" />16         </layout>17         <filter class="org.apache.log4j.varia.LevelRangeFilter">18             <param name="LevelMin" value="DEBUG" />19             <param name="LevelMax" value="ERROR" />20         </filter>21     </appender>

 

Log4j. xml configuration

 

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE log4j: configuration SYSTEM "log4j. dtd">

<Log4j: configuration xmlns: log4j = 'HTTP: // jakarta.apache.org/log4j/'>

<Appender name = "myConsole" class = "org. apache. log4j. leleappender">
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern"
Value = "[% d {dd HH: mm: ss, SSS/} %-5 p] [% t] % c {2/}-% m % n"/>
</Layout>
<! -- Set the output level for the filter -->
<Filter class = "org. apache. log4j. varia. LevelRangeFilter">
<Param name = "levelMin" value = "debug"/>
<Param name = "levelMax" value = "warn"/>
<Param name = "AcceptOnMatch" value = "true"/>
</Filter>
</Appender>

<Appender name = "myFile" class = "org. apache. log4j. RollingFileAppender">
<Param name = "File" value = "D:/output. log"/> <! -- Set the log output file name -->
<! -- Set whether to add a new log to the base of the original log when the service is restarted -->
<Param name = "Append" value = "true"/>
<Param name = "MaxBackupIndex" value = "10"/>
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern" value = "% p (% c: % L)-% m % n"/>
</Layout>
</Appender>

<Appender name = "activexAppender" class = "org. apache. log4j. DailyRollingFileAppender">
<Param name = "File" value = "E:/activex. log"/>
<Param name = "DatePattern" value = "'. 'yyyy-MM-dd'. log'"/>
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern"
Value = "[% d {MMdd HH: mm: ss SSS/} %-5 p] [% t] % c {3/}-% m % n"/>
</Layout>
</Appender>

<! -- Specify logger settings and additivity to indicate whether the default Inheritance Mechanism is followed -->
<Logger name = "com. runway. bssp. activeXdemo" additi.pdf = "false">
<Priority value = "info"/>
<Appender-ref = "activexAppender"/>
</Logger>

<! -- Root logger settings -->
<Root>
<Priority value = "debug"/>
<Appender-ref = "myConsole"/>
<Appender-ref = "myFile"/>
</Root>
</Log4j: configuration>

(1). There are generally five appender output methods:

Org. apache. log4j. RollingFileAppender (rolling file, automatically record the latest log)
Org. apache. log4j. leleappender (console)
Org. apache. log4j. FileAppender (file)
Org. apache. log4j. DailyRollingFileAppender (a log file is generated every day)
Org. apache. log4j. WriterAppender (send log information to any specified place in stream format)



(2) The priority of the diary record, which is divided from high to low.
OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL.
We recommend that you use only FATAL, ERROR, WARN, INFO, and DEBUG levels for Log4j.



(3). The format indicates that parameters in layout start with %, and the following parameters represent different formatting information (parameters are listed in alphabetical order ):
% C indicates the full name of the output class, which can be changed to the dimension output by % d {Num} And Num Class Name (for example, "org. apache. elattings. className ", % C {2} Will output elattings. className)
% D: the log output time is in the format of % d {yyyy-MM-dd HH: mm: ss, SSS}. You can specify the format, for example, % d {HH: mm: ss}
% L location of log event output, including category name, thread, number of lines in the code
% N line break
% M output code specified information, such as info ("message"), Output message
% P output priority, namely, FATAL, ERROR, etc.
% R the number of milliseconds it takes to output the log information from startup to display.
% T name of the thread that outputs the log event

 

 

Xml declaration and DTD

The header of the xml configuration file consists of xml declaration and DTD declaration. The Header Format is as follows:

 

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">          

 

Log4j: configuration (root element)

 

  • Xmlns: log4j[# FIXED attribute]: Define the log4j namespace, take the value "http://jakarta.apache.org/log4j"
  • Appender[* Child]: An appender sub-element defines a log output destination.
  • Logger[* Child]: a logger sub-element defines a log writer.
  • Root[? Child]: the root sub-element defines the root logger.

 

Appender

The appender element defines a log output destination.

 

  • Name[# REQUIRED attribute]: defines the appender name for later reference.
  • Class[# REQUIRED attribute]: defines the full name of the class to which the appender object belongs.
  • Param[* Child]: The parameter passed to the class constructor method when an appender object is created.
  • Layout[? Child]: The layout object used by the appender.

 

Layout

The layout element defines the log formatter associated with an appender.

 

  • Class[# REQUIRED attribute]: defines the full name of the class to which the layout object belongs.
  • Param[* Child]: The parameter passed to the class constructor method when a layout object is created.

 

Logger

The logger element defines a log output device.

 

  • Name[# REQUIRED attribute]: defines the logger name for later reference.
  • Additi.pdf[# ENUM attribute]: The value is "true" (default) or "false". Whether to inherit the attributes of the parent logger.
  • Level[? Child]: defines the Log Level of the logger.
  • Appender-ref[* Child]: defines the output destination of the logger.

 

Root

The root element defines the root logger.

 

  • Param[* Child]: The parameter passed to the class constructor method when the root logger object is created.
  • Level[? Child]: defines the Log Level of the root logger.
  • Appender-ref[* Child]: defines the output destination of the root logger.

 

Level

The level element defines the Log level of the logger object.

 

  • Class[# IMPLIED attribute]: defines the class to which the level object belongs. By default, it is the org. apache. log4j. Level class.
  • Value[# REQUIRED attribute]: assigns a value to the level object. Possible values are "all", "debug", "info", "warn", "error", "fatal", and "off" in ascending order ". When the value is "off", no log information is output.
  • Param[* Child]: parameter passed to the class constructor when a level object is created

 

Appender-ref

The appender-ref element references the name of an appender element and adds an appender to the logger object.

 

  • Ref[# REQUIRED attribute]: A reference to the name of an appender Element
  • The appender-ref element does not have child elements.

 

Param

The param element provides parameters for the class constructor when creating an object. It can be a child element of appender, layout, filter, errorHandler, level, categoryFactory, and root.

 

  • NameAndValue[# REQUIRED attributes]: provides a set of parameter name-value pairs.
  • The param element does not have child elements.
Configure appender and layout in the xml file

You must call different constructor methods to create different Appender objects or Layout objects. You can use the param sub-element to set different parameter values.

Create leleappender object

The constructor of ConsoleAppender does not accept other parameters.

 

... ... ... ...<appender name="console.log" class="org.apache.log4j.ConsoleAppender">  <layout ... >    ... ...  </layout></appender>... ... ... ...          

 

Create a FileAppender object

Two parameters can be passed for the FileAppender class constructor: File indicates the log File name; Append indicates whether to Append the log to the end of the File if the File already exists, optional values: "true" and "false" (default ).

 

... ... ... ...<appender name="file.log" class="org.apache.log4j.FileAppender">  <param name="File" value="/tmp/log.txt" />  <param name="Append" value="false" />  <layout ... >    ... ...  </layout></appender>... ... ... ...          

 

Create a RollingFileAppender object

In addition to File and Append, you can also pass two parameters for the RollingFileAppender class constructor: the number of log files backed up by MaxBackupIndex (1 by default ); maxFileSize indicates the maximum number of bytes allowed by the log file (10 MB by default ).

 

... ... ... ...<appender name="rollingFile.log" class="org.apache.log4j.RollingFileAppender">  <param name="File" value="/tmp/rollingLog.txt" />  <param name="Append" value="false" />  <param name="MaxBackupIndex" value="2" />  <param name="MaxFileSize" value="1024" />  <layout ... >    ... ...  </layout></appender>... ... ... ...          

 

Create a PatternLayout object

You can pass the ConversionPattern parameter for the constructor of the PatternLayout class.

 

... ... ... ...<layout class="org.apache.log4j.PatternLayout>  <param name="Conversion" value="%d [%t] %p - %m%n" /></layout>... ... ... ...

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.