Log4j.xml configuration detailed properties of too much eye.

Source: Internet
Author: User
Tags log4j

XML code


<?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.ConsoleAppender" >
<layout class= "Org.apache.log4j.PatternLayout" >
<param name= "Conversionpattern"
Value= "[%d{dd hh:mm:ss,sss\}%-5p] [%t]%c{2\}-%m%n"/>
</layout>
<!--filter sets the level of output-->
<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 log output file name-->
<!--set whether to add a new log on the basis 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>
</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\}%-5p] [%t]%c{3\}-%m%n"/>
</layout>
</appender>

<!--specify logger settings, additivity indicate whether to follow the default inheritance mechanism-->
<logger name= "Com.runway.bssp.activeXdemo" additivity= "false" >
<priority value = "Info"/>
<appender-ref ref= "Activexappender"/>
</logger>

<!--the setting of root logger-->
<root>
<priority value = "Debug"/>
<appender-ref ref= "MyConsole"/>
<appender-ref ref= "MyFile"/>
</root>
</log4j:configuration>

(1). The output mode Appender generally has 5 kinds:

Org.apache.log4j.RollingFileAppender (scrolling file, automatic recording of the latest log)
Org.apache.log4j.ConsoleAppender (console)
Org.apache.log4j.FileAppender (file)
Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)



(2). The priority level of the diary record is priority, and the priority is divided into
Off, FATAL, ERROR, WARN, INFO, DEBUG, all.
LOG4J recommends using only the five levels of fatal, ERROR, WARN, INFO, and Debug.



(3). The parameters in the format note layout start with a%, followed by different parameters representing different formatting information (the parameters are listed in alphabetical order):
The full name of the class to which the%c output belongs, can be modified to%d{num}, the dimension of the Num class name output (such as: "Org.apache.elathen.ClassName",%c{2} will output elathen. ClassName)
%d output log time in the format of%d{yyyy-mm-dd hh:mm:ss,sss}, you can specify a format such as%d{hh:mm:ss}
%l output Log event occurrence location, including class name, occurrence thread, number of lines in code
%n line break
%m output code to specify information, such as info ("message"), output message
%p output priority, that is, FATAL, ERROR, etc.
%r output the number of milliseconds it takes to display the log information from boot to
%t output The name of the thread that generated the log event

XML declaration and DTD

The header of an XML configuration file consists of two parts: an XML declaration and a DTD declaration. The head 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]: Defines the log4j namespace, takes a fixed value "http://jakarta.apache.org/log4j/" appender [* Child]: A appender child element defines a log output destination logger [* child]: A logger element defines a log writer root [? Child]: The root element defines root logger

Appender

The Appender element defines a log output destination.

name [#REQUIRED attribute]: Define the name of the Appender to be referenced in the following text class [#REQUIRED attribute]: Define the full name of the class to which the Appender object belongs param [* Child]: A parameter passed to the class construction method when the Appender object is created layout [? Child]: The layout object that the Appender uses

Layout

The layout element defines a log formatter that is associated with a appender.

class [#REQUIRED attribute]: Defines the full name of the class to which the layout object belongs param [* child]: Parameters that are passed to the class construction method when the layout object is created

Logger

The logger element defines a log output.

name [#REQUIRED attribute]: Define logger's name so that it is referenced additivity [#ENUM attribute]: value is "true" (default) or "false", Whether to inherit the parent logger 's attribute levels [? Child]: Define the log level of the logger appender-ref [* Child]: Define the output destination of the Logger

Root

The root element defines roots log output root logger.

param [* Child]: The parameter level [. Child] that is passed to the class construction method when the root logger object is created: Defines the log levels for root logger appender-ref [* Child]: definition Output destination for root logger

Level

The level element defines the log levels of the Logger object.

class [#IMPLIED attribute]: Defines the class to which the level object belongs, by default is "Org.apache.log4j.Level class value [#REQUIRED attribute]: Assign a value to a level object." Possible values from small to large in turn are "All", "Debug", "Info", "Warn", "error", "Fatal" and "off." A value of "off" means that no log information is output param [* child]: parameters passed to the class construction method when the level object is created

Appender-ref

The appender-ref element refers to the name of a appender element, adding a appender to the Logger object.

ref [#REQUIRED attribute]: A reference to the name of a appender element appender-ref element has no child elements

param

The Param element provides arguments for the constructor method of the class when the object is created. It can be a child of elements such as appender, layout, filter, ErrorHandler, level, categoryfactory, and Root.

name and value [#REQUIRED attributes]: A set of name values that provide a parameter have no child elements in the PARAM element to configure Appender and layout in the XML file

Create different Appender objects or different layout objects to invoke different construction methods. You can use the Param child element to set different parameter values. Creating Consoleappender Objects

The Consoleappender construction method does not accept other parameters.

... ... ... ...
<appender name= "Console.log" class= "Org.apache.log4j.ConsoleAppender" >
  <layout ... > ...
  </layout> </appender> .......

          

Creating Fileappender Objects

You can pass two parameters for the constructor method of the Fileappender class: file represents the log file name, append indicates that if the file already exists, whether the log is appended to the end of the file, the value may be "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> </appender> .......

          

Creating Rollingfileappender Objects

In addition to file and append, you can pass two parameters for the constructor of the Rollingfileappender class: The number of Maxbackupindex backup log files (default is 1) ; MaxFileSize represents the maximum number of bytes allowed by the log file (10M is the default).

... ... ... ...
<appender name= "RollingFile.log" class= "Org.apache.log4j.RollingFileAppender" >
  <param "File" Value= "/tmp/rollinglog.txt"/>
  <param name= "Append" value= "false"/> <param "name=
  " Maxbackupindex "Value=" "2"/> <param name= "maxfilesize"
  value= "1024"/> <layout
  ...
    ...
  </layout> </appender> .......

          

Creating Patternlayout Objects

You can pass parameter Conversionpattern for the Patternlayout class's construction method.

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

Xml
<context-param>   <param-name>log4jConfigLocation</param-name>   <param-value>/WEB-INF/log4j.xml</param-value>  </context-param>  < Context-param>   <param-name>log4jRefreshInterval</param-name>   < Param-value>60000</param-value>  </context-param> 

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.