log4j-Configuration file

Source: Internet
Author: User
Tags print format

The log4j configuration file is used to set the level, the repository, and the layout of the logger, which can be set in key=value format or in XML format.

By configuration, you can create a running environment for log4j.

1. The basic format of the configuration file log4j configuration file is as follows:

The code is as follows:

1 #配置根Logger2 3 Log4j.rootlogger = [level], appenderName1, appenderName2, ...4 
5 #配置日志信息输出目的地Appender6
7 log4j.appender.appenderName = Fully.qualified.name.of.appender.class8
9 log4j.appender.appenderName.option1 = value1 ...10
One Log4j.appender.appenderName.optionN = Valuen12
- #配置日志信息的格式 (Layout)14
the log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class16
-log4j.appender.appenderName.layout.option1 = value1 ...18
+Log4j.appender.appenderName.layout.optionN = Valuen

Where [level] is the log output levels, there are 5 levels: The code is as follows:

1   FATAL 0  ERROR 3  WARN 4  INFO 6  DEBUG 7  

Appender for the log output destination, LOG4J provides the following types of Appender: The code is as follows:

1   Org.apache.log4j.ConsoleAppender (console), 2 3    org.apache.log4j.FileAppender (file),45    Org.apache.log4j.DailyRollingFileAppender (Generate a log file per day),67    Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size),89   Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)

Layout: Log output format, LOG4J provides the following types of layout:

1   org.apache.log4j.HTMLLayout (Layout in HTML table Form), 2 3   Org.apache.log4j.PatternLayout (flexibility to specify layout mode), 4 5   Org.apache.log4j.SimpleLayout (contains the level of log information and the information string), 6 7   Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log

Printing parameters: log4j Format the log information in a print format similar to the printf function in C, as follows:

1  %m The message specified in the output code2 3 %p output priority, i.e. Debug,info,warn,error,fatal4 5 %r Output The number of milliseconds that the log information is consumed from the application boot to output6 7 %c output belongs to the class, which is usually the full name of the class8 9 %t Output The name of the thread that generated the log eventTen  One %n output A carriage return newline character, Windows platform is "\ r \ n", Unix platform is "\ n" A  - %d the date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyy MMM dd HH:mm:ss, SSS}, output similar: October 18, 2002 22:10:28, 921 -  the%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)

2. Initialize the logger in your code:

1) Call the Basicconfigurator.configure () method in the program: Add a consoleappender to the root logger, and the output format is set to "%-4r [%t]%-5p%c-%m%n" via Patternlayout, The default level of the root logger is level.debug.

2) configuration placed in the file, through the command line parameters to pass the file name, through Propertyconfigurator.configure (Args[x]) parsing and configuration;

3) The configuration is placed in the file, through the environment variables to pass the file name and other information, using the LOG4J default initialization process parsing and configuration;

4) configuration is placed 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 note only the exception level of log output, but usually all levels of output are placed in a file, if the level of log output is bug!?  Then go for it slowly. At this point we may think it would be nice to have the exception message output to a single file. Of course, LOG4J has provided such a function, we only need to modify the configuration of the Appender threshold can be implemented, such as the following example:

The configuration file code is as follows:

1 # # # set log Levels # # #2 3 Log4j.rootlogger = Debug, stdout, D, E4 5 # # # output to console # # #6 7 log4j.appender.stdout = Org.apache.log4j.ConsoleAppender8 9 log4j.appender.stdout.Target = System.outTen  One log4j.appender.stdout.layout = Org.apache.log4j.PatternLayout A  - Log4j.appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%n -  the # # # Output to log file # # # -  - LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender -  + log4j.appender.d.file = Logs/log.log -  + Log4j.appender.d.append = True A  at log4j.appender.d.threshold = DEBUG -  - # # Output debug levels above the log -  - log4j.appender.d.layout = Org.apache.log4j.PatternLayout -  in Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n -  to # # # Save exception information to a separate file # # # +  - LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender the  * log4j.appender.d.file = Logs/error.log $ Panax Notoginseng # # Exception Log file name -  the Log4j.appender.d.append = True +  A log4j.appender.d.threshold = ERROR the  + # # Only output logs above the error 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

1  Public classtestlog4j {2 3       Public Static voidMain (string[] args) {4 5Propertyconfigurator.configure ("D:/code/conf/log4j.properties" );6 7Logger Logger = Logger.getlogger (testlog4j.class );8  9Logger.debug ("Debug" );Ten    OneLogger.error ("Error" ); A  -      } -  the}

Run and see if the exception is stored in a separate file Error.log

log4j-Configuration file

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.