The configuration file of Log4J is used to set the level, storage and layout of the recorder. It can be connected with the setting information in key=value format or xml format. Through configuration, the running environment of Log4J can be created.
1. Configuration file
The basic format of Log4J configuration file is as follows:
#Configure root Logger log4j.rootLogger = [ level ] , appenderName , appenderName1 , ... #Configure log information output destination and Appender options log4j.appender.appenderName = fully.qualified.name.of.appender.class log4j.appender.appenderName.option = value1 ... log4j.appender.appenderName.optionN = valueN #Configure the format (layout) and format layout options of log information appender.appenderName.layout = fully.log4j.qualified.name.of.layout.class log4j.appender.appenderName.layout.option1 = value1 ... log4j.appender.appenderName.layout.optionN = valueN
- Where [level] is the log output level: ERROR, WARN, INFO, DEBUG
ERROR is a serious ERROR, mainly a program ERROR
WARN is a general warning, such as session loss
INFO is the general information to be displayed, such as login and logout
DEBUG is the debugging information of the program - appenderName is the name of the configuration of the log output location
-
log4j.appender.appenderName = fully.qualified.name.of.appender.class in fully.qualified.name.of.appender.class The destination of the following information output shall be replaced:
- org.apache.log4j.ConsoleAppender
- org.apache.log4j.FileAppender (file)
- org. apache. log4j. Daily rollingfileappender (one 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 in stream format to any specified place)
- appender.appenderName.layout = fully.log4j.qualified.name.of.layout.class qualified. name. of. layout. The class format layout should be replaced with the following information:
- org.apache.log4j.HTMLLayout (layout in HTML table form)
- org.apache.log4j.PatternLayout (layout mode can be specified flexibly)
- org.apache.log4j.SimpleLayout (level and information string containing log information)
- org.apache.log4j.TTCCLayout (including log generation time, thread, category, etc.)
- log4j. appender. appenderName. Attribute / option in option that should be replaced
1.ConsoleAppender console options
Threshold=DEBUG: Specifies the lowest level of output for log messages.
ImmediateFlush=true: the default value is true, which means that all messages will be output immediately.
Target=System.err: by default: system Out to specify the output console
2.FileAppender file options
Threshold = debug: Specifies the lowest level of output for log messages.
ImmediateFlush=true: the default value is true, which means that all messages will be output immediately.
File=mylog.txt: Specifies that the message is output to mylog Txt file.
Append=false: the default value is true, that is, the message is added to the specified file. False means that the message overwrites the specified file content.
3.RollingFileAppender generates a file every day option
Threshold=DEBUG: Specifies the lowest level of output for log messages.
ImmediateFlush=true: the default value is true, which means that all messages will be output immediately.
File=mylog.txt: Specifies that the message is output to mylog Txt file.
Append=false: the default value is true, that is, the message is added to the specified file. False means that the message overwrites the content of the specified file.
MaxFileSize=100KB: the suffix can be KB, MB or GB When the log file reaches this size, it will automatically scroll, that is, move the original content to mylog log. 1 document.
MaxBackupIndex=2: Specifies the maximum number of scrolling files that can be generated.
Configuration example
### Configuration root ### log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE ### Set output sql Level of, where logger The following contents are all jar The package name contained in the package ### log4j.logger.org.apache=dubug log4j.logger.java.sql.Connection=dubug log4j.logger.java.sql.Statement=dubug log4j.logger.java.sql.PreparedStatement=dubug log4j.logger.java.sql.ResultSet=dubug ### Configuration output to console ### log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n ### Configuration output to file ### log4j.appender.fileAppender = org.apache.log4j.FileAppender log4j.appender.fileAppender.File = logs/log.log log4j.appender.fileAppender.Append = true log4j.appender.fileAppender.Threshold = DEBUG log4j.appender.fileAppender.layout = org.apache.log4j.PatternLayout log4j.appender.fileAppender.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### The configuration is exported to a file, and a file is created every day ### log4j.appender.dailyRollingFile = org.apache.log4j.DailyRollingFileAppender log4j.appender.dailyRollingFile.File = logs/log.log log4j.appender.dailyRollingFile.Append = true log4j.appender.dailyRollingFile.Threshold = DEBUG log4j.appender.dailyRollingFile.layout = org.apache.log4j.PatternLayout log4j.appender.dailyRollingFile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### When the configuration is output to a file and the size reaches the specified size, a new file is generated ### log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender log4j.appender.ROLLING_FILE.Threshold=ERROR log4j.appender.ROLLING_FILE.File=rolling.log log4j.appender.ROLLING_FILE.Append=true log4j.appender.ROLLING_FILE.MaxFileSize=10KB log4j.appender.ROLLING_FILE.MaxBackupIndex=1 log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ### Configure output to mail ### log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender log4j.appender.MAIL.Threshold=FATAL log4j.appender.MAIL.BufferSize=10 log4j.appender.MAIL.From=chenyl@yeqiangwei.com log4j.appender.MAIL.SMTPHost=mail.hollycrm.com log4j.appender.MAIL.Subject=Log4J Message log4j.appender.MAIL.To=chenyl@yeqiangwei.com log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ### Configuration output to database ### log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver log4j.appender.DATABASE.user=root log4j.appender.DATABASE.password= log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n') log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender log4j.appender.A1.File=SampleMessages.log4j log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j' log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout
ConversionPattern log information, meaning represented by the symbol:
- X number: left alignment when X information is output;
% p: priority of output log information, i.e. DEBUG, INFO, WARN, ERROR, FATAL,
% d: output the date or time of the log time point. The default format is ISO8601. You can also specify the format after it, such as:% d{yyy MMM dd HH:mm:ss,SSS}. The output is similar: 22:10:28921, October 18, 2002
% r: output the number of milliseconds spent from application startup to outputting the log information
% c: the category to which the output log information belongs, usually the full name of the class
% t: output the name of the thread that generated the log event
% l: the location where the output log event occurs, which is equivalent to the combination of% C.%M(%F:%L), including the category name, the thread that occurs, and the number of lines in the code. Example: testlog4 main (TestLog4.java:10)
% x: output NDC (nested diagnostic environment) associated with the current thread, especially for multi client and multi-threaded applications such as java servlets.
%%: output a '%' character
% F: name of the file where the output log message is generated
% L: line number in output code
% m: output the message specified in the code and the specific log information generated
% n: output a carriage return line feed, which is "\ r\n" for Windows platform and "\ n" for Unix platform. Output log information line feed
You can add modifiers between% and pattern characters to control their minimum width, maximum width, and text alignment. For example:
1)%20c: Specifies the name of the output category. The minimum width is 20. If the name of the category is less than 20, it is aligned right by default.
2)%-20c: specify the name of the output category. The minimum width is 20. If the name of the category is less than 20, the "-" sign specifies the left alignment.
3)%.30c: specify the name of the output category. The maximum width is 30. If the name of the category is greater than 30, the extra characters on the left will be truncated, but if it is less than 30, there will be no spaces.
4)%20.30c: if the name of category is less than 20, fill in spaces and align it to the right. If its name is longer than 30 characters, cut off the characters far from the left.