log4j specific output information level configuration method

Source: Internet
Author: User

OG4J specific output information level configuration method

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 levels, there are 5 levels:
FATAL 0
ERROR 3
WARN 4
INFO 6
DEBUG 7


Appender for the log output destinations, LOG4J provides the following appender:
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 the specified size),
Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)


Layout: Log output format, LOG4J provides the following Layout:
Org.apache.log4j.HTMLLayout (layout in HTML form),
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 uses a print format similar to the printf function in C to format log information as follows:
  %m The message specified in the output code
  %p output priority, i.e. Debug,info,warn,error,fatal
  %r Output The number of milliseconds it takes to boot to output the log information
  The class to which the %c output belongs, usually the full name of the class in which it is located
  %t Output The name of the thread that generated the log event
  %n Output a carriage return line break, the Windows platform is "/r/n", and the UNIX platform is "/n"
  %d output log point-in-time date or time, the default format is ISO8601, you can also specify the format after, such as:%d{yyy MMM dd HH:mm:ss, SSS}, output similar: October 18, 2002 22:10:28, 921
  %l The location where the output log event occurs, 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) calling in the programbasicconfigurator.configure ()Method: Add a consoleappender to the root logger, and the output format is set to the Patternlayout"%-4r [%t]%-5p%c%x-%m%n", and the default level of the root logger isLevel.debug.
2 The configuration is placed in the file, pass the file name through the command line parameter, passpropertyconfigurator.configure (args[x])Parse 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 that we only need to modify in the configurationAppenderOfThresholdCan be achieved, 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 = Org.apache.log4j.PatternLayout
Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n


[use in code]
Public   class  TestLog4j  {
     public & nbsp static   void  main (String[] args)   {

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.