Very detailed log4j configuration steps _java

Source: Internet
Author: User
Tags html form print format log4j

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

Copy Code code 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:
Copy Code code as follows:

FATAL 0
ERROR 3
WARN 4
INFO 6
DEBUG 7

Appender for the log output destinations, LOG4J provides the following appender:
Copy Code code as follows:

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 Format the log information using a print format similar to the printf function in C, as follows:
Copy Code code 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 feed, Windows platform "\ r \ n", UNIX platform "\ 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 to invoke 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" by Patternlayout. 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 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]
Copy Code code as follows:

### 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 log above 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 static void Main (string[] args) {
Propertyconfigurator.configure ("D:/code/conf/log4j.properties");
Logger Logger = Logger.getlogger (testlog4j. Class);
Logger.debug ("Debug");
Logger.error ("error");
}
}

Run it and see if the exception information is saved in a separate file Error.log

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.