1. What is log4j
Log4j is an open source project for Apache and is a package for manipulating logs. By using log4j, you can specify destinations for log information output, such as consoles, files, Cui components, NT event loggers, and control each log output format. and are all in the form of a configuration file. What is the composition of 2.log4j?
<1>. Logger: Responsible for generating logs, and can classify the log information to filter, popularly speaking is to decide what log information should output, what log information should be ignored.
<2>. Appender: Define the destination of log information output, specify where the log information should be exported, such as console, file, network equipment, etc.
<3>. Layout: Specifies the output format of the log information.
With the collaboration of three components, the developer can record information according to the log information category, and can control the output format and log storage location of the log while the program is running. A logger can have multiple appender, which means that log information can be exported to multiple devices at the same time, and each appender corresponds to a layout,layout format that determines the output log information. 3. Usage
To introduce a jar package:
Log4j-1.2.11.jar
Src/log4j.properties:
############# log output to the console #############
#通过根元素指定日志输出的级别, Destination
# Log Output priority: Debug < Info < warn < Error
log4j.rootlogger=info,console,file
#日志输出到控制台使用的api类
log4j.appender.console= Org.apache.log4j.ConsoleAppender
#指定日志输出的格式: Flexible format
log4j.appender.console.layout= Org.apache.log4j.PatternLayout
#具体格式的内容
log4j.appender.console.layout.conversionpattern=%d%p%c.%m ()-% m%n
############# Log output to file #############
Log4j.appender.file=org.apache.log4j.rollingfileappender
# file parameters, specifying the path of the log file (here is the logs folder to be exported to Tomcat)
log4j.appender.file.file= ... /logs/mylog.log
# file parameters, specifying maximum log file size
log4j.appender.file.maxfilesize=5kb
# file parameters, specifying the maximum number
of log files generated log4j.appender.file.maxbackupindex=100
log4j.appender.file.layout=org.apache.log4j.patternlayout
log4j.appender.file.layout.conversionpattern=%d%c.%m ()-%m%n
Output to the console implementation. The configuration for the output to the file is not needed, otherwise the file error cannot be reported :
public class App_log4j {
log log = Logfactory.getlog (App_log4j.class);
@Test public
Void Save () {
try {
Log.info (Save: Start to Save method);
int i = 1/0;
Log.info ("Save: Execute save End, success");
} An exception occurred in catch (Exception e) {
log.error ("Execute App Class Save () method." "); Exception
E.printstacktrace ();
}
@Test public
void testlog4j () throws exception{
//output different levels of hint
log.debug ("Debug Information");
Log.info ("information hint");
Log.warn ("warning");
Log.error ("exception");
}
Results:
--------------------------------------------------------------originating from Document:----------------------------------
The meanings represented by 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-in-time, the default format is ISO8601, or the format can be specified thereafter, for example:%d{yyy MMM dd hh:mm:ss,sss}, Output is similar: October 18, 2002 22:10:28,921
%r: The number of milliseconds to output from the application boot to the output of this log information
%c: The class that the output log information belongs to, usually the full name of the class in which it is located
%t: Output The name of the thread that generated the log event
%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 lines in the code. 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 was generated
%l: line number in output code
%M: The message specified in the output code, the resulting log specific information
%n: Output a carriage return line break, Windows platform for "\ r \ n", Unix platform for "\ n" Output log message Wrapping
%m: Represents the name of the method
You can control the minimum width, the maximum width, and the alignment of text by adding modifiers between% and pattern characters. Such as:
1)%20c: Specifies the name of the output category, the minimum width is 20, if the category name is less than 20.
2)%-20c: Specifies 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: Specifies the name of the output category, the maximum width is 30, if the category name is greater than 30, will be the left more characters truncated, but less than 30 words will not have spaces.
4)%20.30c: If the name of the category is less than 20 to fill the space, and the right alignment, if its name is longer than 30 characters, from the left side of the exported characters are cut off.