This article is from http://www.cnblogs.com/tsingxu/archive/2011/11/06/2238453.html
Package com.slf4j.test;
Import org.slf4j.*;
/**
* *
@author Tsingxu * */Public
class Test {public
static final Logger Logger = Loggerfactor Y.getlogger (test.class);
public static void Main (string[] args) {
system.setproperty ("Log4j.configuration", "log4j.properties");
Test.logger.debug ("Test start ...");
Test.logger.error ("Test error ...");
}
In the selection of Slf4j-api-1.6.4.jar + Slf4j-jdk14-1.6.4.jar output in the console:
2011-11-6 21:55:18 com.slf4j.test.test main
严重: test error...
In the selection of Slf4j-api-1.6.4.jar + Slf4j-log4j12-1.6.4.jar + Log4j-1.2.13.jar The output (provided that there is a corresponding configuration file log4j.properties) to the file Logs.log and Errors.log
Logs.log:
2011-11-06 21:59:33 [ main:0 ] - [ DEBUG ] test start... 2011-11-06 21:59:33 [ main:0 ] - [ ERROR ] test error...
Errors.log
2011-11-06 21:59:33 [ main:0 ] - [ ERROR ] test error...
The log4j.properties is briefly configured as follows:
Log4j.debug=true
log4j.rootlogger=debug,d,e
log4j.appender.e = Org.apache.log4j.DailyRollingFileAppender
log4j.appender.e.file = Logs/logs.log
Log4j.appender.e.append = True
Log4j.appender.e.threshold = DEBUG
log4j.appender.e.layout = org.apache.log4j.PatternLayout
Log4j.appender.e.layout.conversionpattern =%-d{ YYYY-MM-DD HH:mm:ss} [%t:%r]-[%p] %m%n
#log4j. Appender.e.layout.
LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender
log4j.appender.d.file = Logs/error.log
Log4j.appender.d.append = True
log4j.appender.d.threshold = ERROR
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
Let's talk about log4j.properties configuration parameters
1 basic formats are 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 =
There are five levels of [level] log output levels
FATAL 0
ERROR 3
WARN 4
INFO 6
DEBUG
Appender for the log output destinations, LOG4J provides the following appender:
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 table),
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:
%m the message specified in the output code%p output priority, that is debug,info,warn,error,fatal
%r Output The number of milliseconds it takes to boot to output the log information
%c
%t
%n
%d output log point 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
%l
Description of the parameter meaning
The type of output level error
for serious errors is mainly a program error
WARN for general warning, such as session loss
info for general to display information, such as login log out
debug for program debugging information
Configure log information output destination
log4j.appender.appendername=??
1.org.apache.log4j.consoleappender (console)
2.org.apache.log4j.fileappender (file)
3. Org.apache.log4j.DailyRollingFileAppender (produces one log file per day)
4. Org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size)
5. Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)
Configure the format of log information
Log4j.appender.appenderName.layout =??
1.org.apache.log4j.htmllayout (layout in HTML table),
2.org.apache.log4j.patternlayout (you can specify layout patterns flexibly),
3. Org.apache.log4j.SimpleLayout (The level and information string that contains the log information),
4.org.apache.log4j.ttcclayout (contains information about the time, thread, category, and so on that the log was generated)
Consoleappender option
threshold=debug: Specifies the lowest level of output for log messages .
immediateflush=true: The default value is true, meaning that all messages will be immediately exported.
Target=system.err: By default: System.out, specify output console
Fileappender option
threshold=debuf: Specifies the lowest level of output for log messages.
immediateflush=true: The default value is true, meaning that all messages will be immediately exported.
file=mylog.txt: Specifies the message output to the Mylog.txt file.
Append=false: The default value is True, the message is incremented to the specified file, and false refers to overwriting the specified file contents with the message.
Rollingfileappender option
threshold=debug: Specifies the lowest level of output for log messages.
immediateflush=true: The default value is true, meaning that all messages will be immediately exported.
file=mylog.txt: Specifies the message output to the Mylog.txt file.
Append=false: The default value is True, the message is incremented to the specified file, and false refers to overwriting the specified file contents with the message.
maxfilesize=100kb: The suffix can be kb, MB, or GB. When the log file reaches that size, it scrolls automatically, moving the original content to the Mylog.log.1 file.
maxbackupindex=2: Specifies the maximum number of scrolling files that can be produced.
The meaning of several symbols in the log Information format:-X
: left-aligned when x information is output;
%p: Output log information priority, i.e. Debug,info,warn,error,fatal,
%d: the date or time of the output log point of time. The default format is ISO8601, or you can specify a format later, such as:%d{yyy MMM dd hh:mm:ss,sss}, output similar to: October 18, 2002 22:10:28,921
%r: The number of milliseconds that the output from the application boot to the output of this log information is to be used for the
class that the output log information belongs to, usually the full name of the class
%t: Output The thread name that generated the log event
%l: The location where the output log event occurred, equivalent to%c.%m (%f:%l , including the class name, the thread that occurred, and the number of lines in the code. For example: Testlog4.main (testlog4.java:10)
%x: The NDC (nested diagnostics environment) associated with the current line threads relative, especially in multiple client multi-threaded applications such as Java Servlets.
percent%: output a "%" character
%F: The name of the file where the output log message is generated
%l: line number in the output code
%m: The message specified in the output code, the log specific information generated
%n: output a carriage return line break, Windows platform is "\ r \ n", the UNIX platform is "\ n" Output log message Wrapping
The configuration file for the sample log4j.properties
Log4j.debug=true
log4j.rootlogger=debug,d,e
log4j.appender.e = Org.apache.log4j.DailyRollingFileAppender
log4j.appender.e.file = Logs/logs.log
Log4j.appender.e.append = True
Log4j.appender.e.threshold = DEBUG
log4j.appender.e.layout = org.apache.log4j.PatternLayout
Log4j.appender.e.layout.conversionpattern =%-d{ YYYY-MM-DD HH:mm:ss} [%t:%r]-[%p] %m%n
#log4j. Appender.e.layout.
LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender
log4j.appender.d.file = Logs/error.log
Log4j.appender.d.append = True
log4j.appender.d.threshold = ERROR
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
When the configuration file is loaded, the console displays the procedure for LOG4J resolution of the configuration file. Errors will Error
Log4j:parsing for [Root] with value=[debug,d,e].
Log4j:level token is [DEBUG].
Log4j:category Root set to DEBUG log4j:parsing appender named "D".
log4j:parsing layout options for "D".
Log4j:setting Property [Conversionpattern] to [%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n].
Log4j:end of parsing for "D".
Log4j:setting property [Threshold] to [ERROR].
log4j:setting [Append] to [true].
log4j:setting property [file] to [Logs/error.log].
Log4j:setfile Called:logs/error.log, True log4j:setfile ended Log4j:appender [D] to is rolled at midnight.
Log4j:parsed "D" options.
Log4j:parsing Appender named "E".
log4j:parsing layout options for "E".
Log4j:setting Property [Conversionpattern] to [%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n].
Log4j:end of parsing for "E".
log4j:setting property [file] to [Logs/logs.log].
Log4j:setting property [Threshold] to [DEBUG].
log4j:setting [Append] to [true]. Log4j:setfile Called:logs/logs.log, True log4j: Setfile ended Log4j:appender [E] to is rolled at midnight.
Log4j:parsed "E" options.
log4j:finished Configuring.