The most detailed log4j configuration in history __java

Source: Internet
Author: User
Tags print format log4j

The log4j configuration file (Configuration file) is used to set the level, the repository, and the layout of the logger, and it can be set in key=value format or in XML format information. By configuring, you can create a log4j running environment.

1. configuration file
The basic format of the log4j configuration file is as follows: #配置根Logger
Log4j.rootlogger = [ level ] , appenderName1, appenderName2, ...

#配置日志信息输出目的地Appender
Log4j.appender.appenderName = Fully.qualified.name.of.appender.class
Log4j.appender.appenderName.option1 = value1
...
Log4j.appender.appenderName.optionN = Valuen

#配置日志信息的格式 (Layout)
Log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class
Log4j.appender.appenderName.layout.option1 = value1
...
Log4j.appender.appenderName.layout.optionN = Valuen


where   [level]  is the log output level, total Level 5:
fatal        0 
error     3 
warn       4 
info         6 
debug      7 

appender  is the destination for the log output, and log4j provides appender in the following ways:
Org.apache.log4j.ConsoleAppender (console),
Org.apache.log4j.FileAppender (file),
Org.apache.log4j.DailyRollingFileAppender (a log file is generated every day),
Org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches a specified size),
Org.apache.log4j.WriterAppender (sending log information in streaming format to any specified place)
Layout : Log output format, LOG4J provides the following Layout:
Org.apache.log4j.HTMLLayout (layout in HTML table),
Org.apache.log4j.PatternLayout (you can specify layout patterns flexibly),
Org.apache.log4j.SimpleLayout (The level and information string that contains the log information),
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)


Print parameters:   log4j Format the log information in a print format similar to the printf function in C, as follows:
     %m      The message specified in the output code
%p    output priority, that is, debug,info,warn,error,fatal 
%r    Output The number of milliseconds it takes to output the log information from the application  
% c    the class to which the output belongs. is typically the full name of the class in which the  
%t    outputs the thread name that generated the log event  
%n    Output a carriage return line break, the Windows platform is "\ r \ n", the UNIX platform is "\ n"  
%d    the date or time of the output log point-in-time, the default format is ISO8601, You can also specify the format later, for example:%d{yyy MMM dd hh:mm:ss , sss}, Output is similar: October 18, 2002  22 : 10 : 28  , 921 
The location of the%l    output log event, including the class name, the thread that occurred, and the number of lines in the code. Example: Testlog4.main (testlog4.java: 10 )  


2. Initialize logger in code:
1 in the program call the basicconfigurator.configure () method: Add a consoleappender to the root logger, the output format through Patternlayout set to "%-4r [%t]%- 5p%c%x-%m%n, and the default level of the root logger is level.debug.
2) configuration is placed in the file, through the command line parameters to pass the file name, through propertyconfigurator.configure (args[x]) to resolve and configure;
3 configuration in the file, through the environment variables to pass the file name and other information, using log4j default initialization process to resolve and configure;
4 configuration in the file, through the application server configuration to pass the file name and other information, using a special servlet to complete the configuration.

3. Set the log output level for different appender:
When debugging a system, we tend to notice only the abnormal level of log output, but usually all levels of output are placed in a file, if the level of log output is a bug. Then go and find it slowly.
At this point, we might think how good it would be to output the exception information to a single file. Of course, LOG4J has provided such a feature, we only need to modify the Appender in the configuration of the Threshold can be implemented, such as the following example:

[configuration file]### set Log Levels ###
Log4j.rootlogger = Debug, stdout, D, E

### Output to console ###
Log4j.appender.stdout = Org.apache.log4j.ConsoleAppender
Log4j.appender.stdout.Target = System.out
Log4j.appender.stdout.layout = Org.apache.log4j.PatternLayout
Log4j.appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%n

### output to log file ###
LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.d.file = Logs/log.log
Log4j.appender.d.append = True
LOG4J.APPENDER.D.Threshold= DEBUG# # Output A log above the debug level
Log4j.appender.d.layout = Org.apache.log4j.PatternLayout
Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss}[%t:%r]-[%p]%m%n

### Save exception information to a separate file ###
LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.d.file = logs/error.log # # Exception Log file name
Log4j.appender.d.append = True
LOG4J.APPENDER.D.Threshold= ERROR# # Only logs above the error level are OUTPUT!!!
Log4j.appender.d.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.