log4j.xml common configuration

Log4J configuration file (Configuration File) is used to set the level recorder, font and layout, which can be accessed by setting information or key = xml format value format. By the configuration, you can create an operating environment of Log4J.

 

A, log4j.jar package Download: http://logging.apache.org/log4j/1.2/download.html

Second, the configuration file type: log4j configuration file can be log4j.xml can also be a log4j.properties

 

log4j.xml configuration instructions

Xml Code   Collection Code
  1. <?xml version="1.0" encoding="UTF-8"?>       
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">       
  3.           
  4. <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' >       
  5.           
  6.     <appender name="myConsole" class="org.apache.log4j.ConsoleAppender">       
  7.         <layout class="org.apache.log4j.PatternLayout">       
  8.             <param name="ConversionPattern"          
  9.                 value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />       
  10.         </layout>       
  11.         <! - set the output level of the filter ->       
  12.         <filter class="org.apache.log4j.varia.LevelRangeFilter">       
  13.             <param name="levelMin" value="debug" />       
  14.             <param name="levelMax" value="warn" />       
  15.             <param name="AcceptOnMatch" value="true" />       
  16.         </filter>       
  17.     </appender>       
  18.        
  19.     <appender name="myFile" class="org.apache.log4j.RollingFileAppender">          
  20.         < Param  name = "File"  value = "D: /output.log"  /> <- Set the log output file name -!>       
  21.         <! - whether set when you restart the service, based on the original log Add New Log ->       
  22.         <param name="Append" value="true" />       
  23.         <param name="MaxBackupIndex" value="10" />       
  24.         <layout class="org.apache.log4j.PatternLayout">       
  25.             <param name="ConversionPattern" value="%p (%c:%L)- %m%n" />       
  26.         </layout>       
  27.     </appender>       
  28.          
  29.     <appender name="activexAppender" class="org.apache.log4j.DailyRollingFileAppender">       
  30.         <param name="File" value="E:/activex.log" />         
  31.         <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />         
  32.         <layout class="org.apache.log4j.PatternLayout">       
  33.          <param name="ConversionPattern"         
  34.             value="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" />       
  35.         </layout>         
  36.     </appender>       
  37.           
  38.     <! - Specifies the logger settings, additivity indicating whether the default inheritance mechanism to follow ->       
  39.     <logger name="com.runway.bssp.activeXdemo" additivity="false">       
  40.         <priority value ="info"/>         
  41.         <appender-ref ref="activexAppender" />         
  42.     </logger>       
  43.        
  44.     <! - Root logger settings ->       
  45.     <root>       
  46.         <priority value ="debug"/>       
  47.         <appender-ref ref="myConsole"/>       
  48.         <appender-ref ref="myFile"/>          
  49.     </root>       
  50. </log4j:configuration>  

 

(1) Output appender There are five general:

org.apache.log4j.RollingFileAppender (scroll through the files automatically records the latest log)

org.apache.log4j.ConsoleAppender (console)

org.apache.log4j.FileAppender (file)

org.apache.log4j.DailyRollingFileAppender (produce a daily log file)

Org.apache.log4j.WriterAppender (log stream format information is sent to any designated place) 


Priority priority (2). Journaling, descending into priority

OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALL。

Log4j is recommended that only FATAL, ERROR, WARN, INFO, DEBUG five levels.

 

(3) Description format of layout parameters are to begin%, different latter parameter represents formatting information (parameters are listed in alphabetical order):

% C belongs to the class of the output of the full name, may be modified to% d {Num}, Num-dimensional output class name (eg: "org.apache.elathen.ClassName",% C {2} The output elathen.ClassName)

% D output log time format of% d {yyyy-MM-dd HH: mm: ss, SSS}, to specify the format such as% d {HH: mm: ss} 

% L output log incident location, including categories were the threads occurs, the number of lines in the code 

% N newline

% M output code specifying information, such as the info ( "message"), the output message

% P output priority, i.e. FATAL, ERROR, etc.

% R output the log information on the number of milliseconds spent from the start to the display

% T output to produce the event log name of thread

 

Detailed profiles node log4j.xml

 

xml declaration and DTD

Head xml configuration file consists of two parts: xml declaration and DTD declaration. Header format is as follows:

 

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

 

 

log4j:configuration (root element)

  • xmlns: log4j [#FIXED attribute]: namespace defined log4j, takes value "http://jakarta.apache.org/log4j/"
  • appender [* child]: appender a child element defines a log output destination
  • logger [* child]: a child element defines a logger device to write log
  • root [child?]: root sub-element defines the root logger

 

appender

appender element defines a log output destination.

  • name [#REQUIRED attribute]: appender defined names so as to be referenced later
  • Full name is defined appender object belongs to the class: class [#REQUIRED attribute]
  • Parameters passed to the constructor of the class objects when creating appender: param [* child]
  • layout [child?]: the use of layout objects appender

 

layout

layout element defines a particular appender associated with log formatter.

  • Full name is defined layout object belongs to the class: class [#REQUIRED attribute]
  • Parameters passed to the constructor when creating a layout object class: param [* child]

 

logger

logger element defines a log output device.

  • name [#REQUIRED attribute]: name defined logger, so as to be referenced later
  • additivity [#ENUM attribute]: the value is "true" (default) or "false", inherit the property of the parent logger
  • level [? child]: the definition of the logger's log level
  • appender-ref [* child]: defines the destination of the output of the logger

 

root

root element defines the root log exporter root logger.

  • Parameters passed to the constructor of the class objects when creating a root logger: param [* child]
  • level [child?]: define the root logger's log level
  • appender-ref [* child]: the output destination of the definition of the root logger

 

level

level element defines the log level logger object. 

  • class [#IMPLIED attribute]: object belongs to a class definition level, default is "org.apache.log4j.Level class
  • value [#REQUIRED attribute]: assigned level objects. Possible values ​​from small to large order of "all", "debug", "info", "warn", "error", "fatal" and "off". It indicates that no log information is output when the value is "off"
  • Parameters passed to the constructor of the class level when creating an object: param [* child]

 

appender-ref

appender-ref element references a name appender elements, increase appender is a logger object.

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

 

param

param element provides parameters for the constructor of the class in object creation. It can be a child element appender, layout, filter, errorHandler, level, categoryFactory and other elements of the root.

  • name and value [#REQUIRED attributes]: providing a set of parameter name-value pairs
  • param element has no child elements

 

Appender and layout configuration in the xml file

Appender create different objects or different Layout object to invoke a different constructor. Param element may be used to set the different sub-parameters.

Creating objects ConsoleAppender

ConsoleAppender constructor does not accept the other parameters.

 

Xml Code   Collection Code
  1. ... ... ... ...  
  2. <appender name="console.log" class="org.apache.log4j.ConsoleAppender">  
  3.   <layout ... >  
  4.     ... ...  
  5.   </layout>  
  6. </appender>  
  7. ... ... ... ...  

 

 

Creating objects FileAppender

Can be passed to the constructor class FileAppender two parameters: File name indicates that the log file; represents the Append The file already exists, whether the file is appended to the tail of the log, the possible values ​​for the "true" and "false" (default).

 

Xml Code   Collection Code
  1. ... ... ... ...  
  2. <appender name="file.log" class="org.apache.log4j.FileAppender">  
  3.   <param name="File" value="/tmp/log.txt" />  
  4.   <param name="Append" value="false" />  
  5.   <layout ... >  
  6.     ... ...  
  7.   </layout>  
  8. </appender>  
  9. ... ... ... ...  

 

 

Creating objects RollingFileAppender

In addition to File and Append, RollingFileAppender class constructor can also pass two parameters: the number MaxBackupIndex backup log files (default 1); represents the MaxFileSize log file allows the maximum number of bytes (default 10M).

 

Xml Code   Collection Code
  1. ... ... ... ...  
  2. <appender name="rollingFile.log" class="org.apache.log4j.RollingFileAppender">  
  3.   <param name="File" value="/tmp/rollingLog.txt" />  
  4.   <param name="Append" value="false" />  
  5.   <param name="MaxBackupIndex" value="2" />  
  6.   <param name="MaxFileSize" value="1024" />  
  7.   <layout ... >  
  8.     ... ...  
  9.   </layout>  
  10. </appender>  
  11. ... ... ... ...  

 

Creating objects PatternLayout

Parameters can be passed to the constructor ConversionPattern PatternLayout class.

Xml Code   Collection Code
  1. ... ... ... ...  
  2. <layout class="org.apache.log4j.PatternLayout>  
  3.   <param name="Conversion" value="%d [%t] %p - %m%n" />  
  4. </layout>  
  5. ... ... ... ...  

demo:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!-- appender 是可用的日志输出方式定义,可以定义多个 -->
<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="C:/nmj/workspace/myWeb/webapp/WEB-INF/logs/info.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="10000KB" />
<param name="MaxBackupIndex" value="50" />
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %d{ISO8601}-- %p -- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<appender name="WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="C:/nmj/workspace/myWeb/webapp/WEB-INF/logs/warn.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="10000KB" />
<param name="MaxBackupIndex" value="50" />
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %d{ISO8601}-- %p -- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="C:/nmj/workspace/myWeb/webapp/WEB-INF/logs/error.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="10000KB" />
<param name="MaxBackupIndex" value="50" />
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %d{ISO8601}-- %p -- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<!-- root部分定义了log4j的默认输出级别和方式 -->
<root>
<priority value="INFO" />
<appender-ref ref="INFO" />
<appender-ref ref="WARN" />
<appender-ref ref="ERROR" />

</root>
</log4j:configuration>

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/muxi0407/p/11589017.html