reprint: http://zhangjunhd.blog.51cto.com/113473/21014/
1.log4j
Introduction log4j is an open source project for Apache that allows developers to output log information at any interval. LOG4J consists of three major categories of components:1) logger-is responsible for output log information, and can classify and filter the log information, that is, decide which log information should be output, which should be ignored. Loggers Component output log information is divided into 5 levels: DEBUG, INFO, WARN, ERROR, FATAL. The order of these five levels is: Debug<info<warn<error<fatal. If the level of a logger component is set to P, only log information with a higher level than p can be output. Logger are inherited, and the topmost is Rootlogger, and the other logger defined will inherit Rootlogger. 2) appender-defines the log output destination and specifies where the log information should be exported. The destination of the output can be a console, file, or network device. 3) layout-the formatted output by attaching the Layout to the back of the Appender. A logger can have multiple appender, and each appender corresponds to a layout.
2.Loggersdefinition Format for logger: log4j. [Loggername]=[level],appendername,appendername,...the level here refers to the logger priority, Appendername is the output of the log information, you can define multiple outputs at the same time.
3.Appendersappender The definition format:log4j.appender.appenderName = Fully.qualified.name.of.appender.class//"Fully.qualified.name.of.appender.class" can specify one of the following five destinations:
Appender class and its role list
| Appender class Name |
Role |
| Org.apache.log4j.ConsoleAppender |
Output logs to the console |
| Org.apache.log4j.FileAppender |
Output a log to a file |
| Org.apache.log4j.DailyRollingFileAppender |
Generate a log file 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 location in stream format |
1) consoleappender option-threshold=warn: Specifies the lowest level of output for log messages. -immediateflush=true: The default value is true, meaning that all messages will be output immediately. -target=system.err: By default: System.out, specify the output console. 2) fileappender option-threshold=warn: Specifies the lowest level of output for log messages. -immediateflush=true: The default value is true, meaning that all messages will be output immediately. -file=mylog.txt: Specifies the message output to the Mylog.txt file. -Append=false: The default value is True, the message is added to the specified file, and false means that the message is overwritten with the specified file content. 3) dailyrollingfileappender option-threshold=warn: Specifies the lowest level of output for log messages. -immediateflush=true: The default value is true, meaning that all messages will be output immediately. -file=mylog.txt: Specifies the message output to the Mylog.txt file. -append=false: The default value is True, the message is added to the specified file, and false refers to overwriting the message with the specified file content. -datepattern= '. ' YYYY-WW: Scrolls a file once a week, which results in a new file every week. You can also specify monthly, weekly, Days, hours, and minutes. The corresponding format is as follows:'. ' YYYY-MM: Monthly'. ' YYYY-WW: Weekly'. ' YYYY-MM-DD: Every day'. ' Yyyy-mm-dd-a: Two times a day'. ' Yyyy-mm-dd-hh: Per hour'. ' Yyyy-mm-dd-hh-mm: Per minute 4) rollingfileappender option-threshold=warn: Specifies the lowest level of output for log messages. -immediateflush=true: The default value is true, meaning that all messages will be output immediately. -file=mylog.txt: Specifies the message output to the Mylog.txt file. -Append=false: The default value is True, the message is added to the specified file, and false means that the message is overwritten with the specified file content. -maxfilesize=100kb: The suffix can be kb, MB, or GB. When the log file reaches this size, it will automatically scroll to move the original content to the Mylog.log.1 file. -maxbackupindex=2: Specifies the maximum number of scroll files that can be produced.
4.LayoutsLayout definition Format:Part One log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class//"Fully.qualified.name.of.layout.class" can specify one of the following 4 formats:layout class and its role list
| Layout class Name |
Role |
| Org.apache.log4j.HTMLLayout |
Layout in HTML table format |
| Org.apache.log4j.PatternLayout |
Flexibility to specify layout mode |
| 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 of log generation |
1) htmllayout option-locationinfo=true: The default value is False, and the output Java file name and line number. -title=my App File: The default value is log4j Log Messages. 2) patternlayout option-conversionpattern=%m%n: Specifies how the specified message is formatted. 3) xmllayout option-locationinfo=true: Default value is false, output Java file and line number. Part Two log4j.appender.a1.layout.conversionpattern=%-4r%-5p%d{yyyy-mm-dd HH:mm:ssS}%c%m%nWhat you need to explain here is the meaning of several symbols in the log Information format:1)-X: Left-justified when the information is output. 2)%p: Output log information priority, that is, debug,info,warn,error,fatal. 3)%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 to: October 18, 2002 22:10:28, 921. 4)%r: The number of milliseconds to output the log information from the application boot to output. 5)%c: The output log information belongs to the class, which is usually the full name of the class. 6)%t: Outputs the name of the thread that generated the log event. 7)%l: The location of the output log event, equivalent to the combination of%c.%m (%f:%l), including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10). 8)%x: The NDC (nested diagnostic environment) associated with the output and current line threads, especially for multi-client multithreaded applications such as Java Servlets. 9) Percent: output a "%" character. %F: The name of the file where the output log message was generated. One )%l: The line number in the output code. %m: Output The message specified in the code, resulting in the log specific information. %n: Output a carriage return line break, Windows platform is "\ r \ n", Unix platform for "\ n" output log information wrapping. you can add modifiers between% and pattern characters to control their minimum width, maximum width, and text alignment. such as:1)%20c: Specify the name of the output category, the minimum width is 20, if the category name is less than 20, the default is the right alignment. 2)%-20c: Specify the name of the output category, the minimum width is 20, if the category name is less than 20, the "-" number specifies left-aligned. 3)%.30c: Specify the name of the output category, the maximum width is 30, if the category name is greater than 30, will be the left more than the character of the cut off, but less than 30, there will be no spaces. 4)%20.30c: If the category name is less than 20, fill in the blanks, and right-aligned, if its name is longer than 30 characters, it is exported from the left hand-sold characters are truncated.
5.Apache Log
Introduction Set [1]
Apache log4j configuration Instructions [2]
Apache log4j usage Example [3]
Apache commons-logging Usage Example [4]
how to self-build Appender extended log4j Framework
6.
references [1] Zhao Qiang, proficient in JSP programming, electronic industry publishing house[2] log4j configuration process
log4j Configuration Full Description