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 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>
- <! - set the output level of 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 -!>
- <! - whether set when you restart the service, based on the original log Add New Log ->
- <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\} %-5p] [%t] %c{3\} - %m%n" />
- </layout>
- </appender>
- <! - Specifies the logger settings, additivity indicating whether the default inheritance mechanism to follow ->
- <logger name="com.runway.bssp.activeXdemo" additivity="false">
- <priority value ="info"/>
- <appender-ref ref="activexAppender" />
- </logger>
- <! - Root logger settings ->
- <root>
- <priority value ="debug"/>
- <appender-ref ref="myConsole"/>
- <appender-ref ref="myFile"/>
- </root>
- </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 version="1.0" encoding="UTF-8" ?>
- <!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.
- ... ... ... ...
- <appender name="console.log" class="org.apache.log4j.ConsoleAppender">
- <layout ... >
- ... ...
- </layout>
- </appender>
- ... ... ... ...
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).
- ... ... ... ...
- <appender name="file.log" class="org.apache.log4j.FileAppender">
- <param name="File" value="/tmp/log.txt" />
- <param name="Append" value="false" />
- <layout ... >
- ... ...
- </layout>
- </appender>
- ... ... ... ...
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).
- ... ... ... ...
- <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>
- ... ... ... ...
Creating objects PatternLayout
Parameters can be passed to the constructor ConversionPattern PatternLayout class.
- ... ... ... ...
- <layout class="org.apache.log4j.PatternLayout>
- <param name="Conversion" value="%d [%t] %p - %m%n" />
- </layout>
- ... ... ... ...
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>