log4j XML configuration

Source: Internet
Author: User
Tags log4j
<?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 cl ass= "Org.apache.log4j.PatternLayout" > <param name= "Conversionpattern" "value=" [%d{d D 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.RollingFileAp  
Pender "> <param name=" file "value=" D:/output.log "/><!--set log output file name-->        <!--settings to add a new log on the basis of the original log--> <param name= "Append" value= "true"/> When the service is restarted <param Name= "Maxbackupindex" value= "ten"/> <layout class= "Org.apache.log4j.PatternLayout" > < param name= "Conversionpattern" value= "%p (%c:%l)-%m%n"/> </layout> </appender> & Lt;appender name= "Activexappender" class= "Org.apache.log4j.DailyRollingFileAppender" > <param name= "File" V Alue= "E:/activex.log"/> <param name= "Datepattern" value= "'. ' Yyyy-mm-dd '. Log ' "/> <layout class=" org.apache.log4j.PatternLayout "> <param name=" conver   
    Sionpattern "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.a Ctivexdemo "additivity=" false "> <priority value =" inFo "/> <appender-ref ref=" Activexappender "/> </logger> <!--root logger settings--> <root> <priority value = "Debug"/> <appender-ref ref= "MyConsole"/> &L  T;appender-ref ref= "MyFile"/> </root> </log4j:configuration>
Configuration parameter Description:
(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 one log file per 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 its format is%d{yyyy-mm-dd hh:mm:ss,sss}, you can specify the format such as%d{hh:mm:ss}
                %l        output Log event occurrence location, including class name, occurrence thread , the number of lines in the code
                %n
                %m      Output Code specify information, such as info ("message"), output message
                %p       output priority, that is, FATAL, ERROR, etc.
                % R        Output The number of milliseconds from boot to the log information
                %t output the        thread name that generated the log event


XML configuration file Detailed:

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE log4j:configuration SYSTEM "Log4j.dtd" > Log4j:configuration (root element) xmlns:log4j [#FIXED att Ribute]: Defines the log4j namespace, takes a fixed value of "http://jakarta.apache.org/log4j/" appender [* child]: A Appender sub element defines a log output destination name [#RE quired attribute]: Define Appender's name so that it can be referenced by the following class [#REQUIRED attribute]: Define the full name of the class to which the Appender object belongs param [* Child]: Create append


				An ER object is passed to the class construction method's parameter layout [? Child]: The layout element defines the log formatter associated with a appender. class [#REQUIRED attribute]: Defines the full name of the class to which the layout object belongs param [* Child]: parameters passed to the class construction method when the layout object was created logger [* child]: a Logge R child element defines a log writer name [#REQUIRED attribute]: Defines the name of the logger so that it is referenced additivity [#ENUM attribute]: value is "true" (default) or "false" Whether to inherit the parent logger's attribute level [. Child]: Define the log levels for the logger appender-ref [* Child]: Define the output destination of the logger root [? child]: root element set The root logger param [* child]: The parameter level passed to the class construction method when the root logger object was created [? Child]: Defines the log-class class for Root logger [#IMPLIED a Ttribute]: Defining LevThe class that the El object belongs to, by default, is the Org.apache.log4j.Level class value [#REQUIRED attribute]: Assigns 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]: A parameter passed to the class construction method when the level object was created Appender-ref [* child]: The element references a appender element
 
 Name, add a appender to the Logger object.




param element has no child elements
Configuring Appender and layout in an 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 "/>"
</layout>



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.