log4j configuration supplement

Log4j component composition 

 

Log4j consists of three important components:

 

1. Priority of log information (Logger)

2. Output destination of log information (Appender)

3. The output format (Layout) of the log information.

 

Overview:

 

The priority of log information is ERROR, WARN, INFO, DEBUG from high to low, which are used to specify the importance of this log information;

 

The output destination of the log information specifies whether the log will be printed to the console or to a file;

 

The output format controls the display content of the log information.

 

Introduction to Log4j

 

  Log4j is an open source project of Apache. By using Log4j, we can control the destination of log information delivery to consoles, files, GUI components, and even socket servers, NT event recorders, UNIX Syslog daemons, etc.; We can also control the output format of each log. By defining the level of each log information, we can control the log generation process in more detail. log4j--log for java (java log) .

 

Log4j download address: http://logging.apache.org/log4j/2.x/download.html

 

 

Format of the Log4j configuration file

 

Log4j supports two configuration file formats:

 

1. XML format file

2. A file in properties format

 

It is also possible to configure the Log4j environment in code without using a configuration file at all. However, using configuration files will make your application more flexible.

 

 

 

Log4j definition configuration file

 

1. Configure the root Logger

 

Its syntax is: 

log4j.rootLogger = [ level ] , appenderName, appenderName, …

 

Parameter Description:  

 

level is the priority of logging, divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or the level you define.

 

Off: The highest level, used to turn off all logging

 

Fatal: Indicates that every critical error event will cause the application to exit.

 

Error: Indicates that although an error event occurs, it still does not affect the continued operation of the system.

 

Warn: Indicates a potential error situation

 

Info: Generally used at a coarse-grained level, emphasizing the entire running of the application

 

Debug: Generally and at a coarse-grained level, emphasizes the entire runtime of the application.

 

All: The lowest level, used to turn on all logging.

 

Log4j recommends using only four levels, the priority from high to low is ERROR, WARN, INFO, DEBUG. With the level defined here, you can control the switch to the corresponding level of log information in the application.

 

The appenderName refers to where the log information is output, and multiple output destinations can be specified at the same time.

 

 

 

2. Configure the log information output destination Appender

 

Its syntax is:

log4j.appender.appenderName = fully.qualified.name.of.appender.class   

log4j.appender.appenderName.option1 = value1   

...   

log4j.appender.appenderName.option = valueN

 

 

The appenders provided by Log4j are 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 (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 stream format)

 

 

 

3. Configure the format of the log information

 

The syntax is:

log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class

log4j.appender.appenderName.layout.option1 = value1 …

log4j.appender.appenderName.layout.option = valueN

 

 

The layouts provided by Log4j are as follows:

 

  org.apache.log4j.HTMLLayout (layout in HTML table),

 

  org.apache.log4j.PatternLayout (the layout pattern can be specified flexibly),

 

  org.apache.log4j.SimpleLayout (contains the level and info string of the log info),

 

  org.apache.log4j.TTCCLayout (including log generation time, thread, category, etc.)

 

 

 

Log4J uses a print format similar to the printf function in C language to format log information. The print parameters are as follows:

 

%m prints the message specified in the code  

 

%p output priority, ie DEBUG, INFO, WARN, ERROR, FATAL   

 

%r The number of milliseconds it took to output the log information since the application was started   

 

%c The category to which the output belongs, usually the full name of the category in which it belongs   

 

%t output the name of the thread that generated the log event   

 

%n prints a carriage return line feed, "rn" for Windows platforms and "n" for Unix platforms   

 

%d Output the date or time of the log time point, the default format is ISO8601, you can also specify the format after it, for example: %d{yyy MMM dd HH:mm:ss,SSS}, the output is similar to: October 18, 2002 22:10:28, 921   

 

%l Output where the log event occurred, including the category name, the thread in which it occurred, and the number of lines in the code. Example: Testlog4.main(TestLog4.java:10)

 

%x: Output the NDC (Nested Diagnostic Context) associated with the current thread, especially used in multi-client multi-threaded applications like java servlets.

 

%%: output a "%" character %F: output the name of the file where the log message was generated

 

%L: line number in output code

 

%m: Output the message specified in the code, and generate log specific information

 

%n: output a carriage return line feed, "\r\n" for Windows platform, "\n" for Unix platform to output log information newline can add modifiers between % and mode characters to control its minimum width, maximum width Width, and alignment of text.

 

Such as:

 

1) c: Specify the name of the output category. The minimum width is 20. If the name of the category is less than 20, it is right-aligned 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 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) .30c: If the name of the category is less than 20, it will be filled with spaces and right-aligned. If the name of the category is longer than 30 characters, it will be truncated from the left character.

 

 

 

This article is taken from: http://www.cnblogs.com/dennisit/archive/2013/01/01/2841603.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327039396&siteId=291194637