1. Configuration file
The basic format of the Log4J configuration file is as follows:
Copy the code code as follows:
#Configure root Logger
log4j.rootLogger = [ level ] , appenderName1 , appenderName2 , …
#Configure log information output destination Appender
log4j.appender.appenderName = fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1 = value1
…
log4j.appender.appenderName.optionN = valueN
#Configure the format (layout) of log information
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, there are 5 levels in total:
Copy the code code as follows:
FATAL 0
ERROR 3
WARN 4
INFO 6
DEBUG 7
Appender is the log output destination. The appenders provided by Log4j include the following:
Copy the code code as follows:
org.apache.log4j.ConsoleAppender(console),
org.apache.log4j.FileAppender(file),
org.apache.log4j.DailyRollingFileAppender (generates a log file every day),
org.apache.log4j.RollingFileAppender (generates a new file when the file size reaches the specified size),
org.apache.log4j.WriterAppender (sends log information in streaming format to any specified place)
Layout: Log output format. The layouts provided by Log4j are as follows:
org.apache.log4j.HTMLLayout (layout in HTML table format),
org.apache.log4j.PatternLayout (can flexibly specify layout patterns),
org.apache.log4j.SimpleLayout (contains the level and information string of the log information),
org.apache.log4j.TTCCLayout (contains log generation time, thread, category, etc. information)
Printing parameters: Log4J uses a printing format similar to the printf function in C language to format the log information, as follows:
Copy the code code as follows:
%m Output the message specified in the code %p Output priority, that is, DEBUG, INFO, WARN, ERROR, FATAL
%r Output the number of milliseconds it took from the application startup to outputting the log information %c Output the category to which it belongs, usually the full name of the class %t Output the name of the thread that generated the log event %n Output a carriage return and line feed character, Windows Platform is "/r/n", Unix platform is "/n"
%d outputs the date or time of the log time point. The default format is ISO8601. You can also specify the format afterwards, such as: %d{yyy MMM dd HH:mm:ss, SSS}. The output is similar to: October 18, 2002. 22:10:28, 921
%l Outputs the location where the log event occurred, including the category name, the thread where it occurred, and the line number in the code. Example: Testlog4.main(TestLog4.java: 10)
2. Initialize Logger in code: 1) Call the BasicConfigurator.configure() method in the program: add a ConsoleAppender to the root recorder, set the output format to "%-4r [%t] %-5p %c %x - %m%n" through PatternLayout, and also The default level for rooted loggers is Level.DEBUG.
2) The configuration is placed in the file, the file name is passed through the command line parameters, and parsed and configured through PropertyConfigurator.configure(args[x]);
3) The configuration is placed in the file, the file name and other information are passed through environment variables, and the default initialization process of log4j is used to parse and configure;
4) The configuration is placed in the file, the file name and other information are passed through the application server configuration, and a special servlet is used to complete the configuration.
3. Set the log output level for different Appenders:
When debugging the system, we often pay attention to only the exception level log output, but usually all levels of output are placed in one file. If the log output level is BUG! ? Then go find it slowly.
At this time, we may think that it would be great if we could output the exception information to a file separately. Of course, Log4j already provides such a function. We only need to modify the Appender's Threshold in the configuration to achieve it, such as the following example:
[configuration file]
Copy the 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 logs 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 output logs above 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
[Used 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