Log4j foundation

xiaoxiao2021-03-06  91

Log4j uses the print format of the Printf function in the C language, and the print parameters are shown in Table 1 as follows:

Message specified in% m output code

% P output priority, Debug, Info, Warn, Error, Fatal

% R Output Self-Application Start to Output The log information takes milliseconds

% C Output The category belongs, usually the full name of the class

% T output thread name generated by the log event

% N outputs a carriage return, the Windows platform is "/ r / n", the UNIX platform is "/ N"

The date or time of the% D output log time, the default format is ISO8601, or it can be specified in the format, such as:% D {YYY MMM DD HH: MM: SS, SSS}, Output: October 18, 2002 22: 10: 28, 921

% L Outputs the occurrence of the log event, including the category name, the thread that occurs, and the number of rows in the code. Example: Testlog4.main (Testlog4.java: 10)

Basic application

Log4j configuration

Now look at the meaning of the log4j.properties configuration file. The first line specifies the level of the root Logger is Debug and outputs this specified to A1. A1 is the second line defined org.apache.log4j.consoleAppender, which indicates that the A1 is output to the console. The third line specifies the format of the output to the A1 to org.apache.log4j.patternlayout. The fourth line specifies the conversion mode output to the A1 format to org.javaresearch.log4j.testlog4j.

Many mature server class software log information will be output to the console while outputting to log file check. This feature can be easily accomplished by modifying the profile without changing any code without changing any code. The relevant configuration files are as follows:

#### Use Two appenders, One to log to console, Another to log to a file

Log4j.rootcategory = Debug, Stdout, R

# Print Only Messages of Priority Warn or Higher For Your Category

Log4j.category.Your.category.Name = WARN

#### First appender Writes To Console

Log4j.appender.stdout = org.apache.log4j.consoleAppender

Log4j.Appender.stdout.Layout = Org.apache.log4j.patternlayout

# Pattern to output the caller's file name and line number.

Log4j.Appender.stdout.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N

#### Second Appender Writes to a file

Log4j.Appender.r = org.apache.log4j.rollingfileappender

Log4j.Appender.r.file = example.log

# Control The Maximum log file size

Log4j.Appender.r.maxfilesize = 100kb

# Archive log files (One Backup File Here)

Log4j.Appender.r.maxbackupindex = 1

Log4j.Appender.r.Layout = org.apache.log4j.patternLayoutLog4j.Appender.r.Layout.conversionPattern =% P% T% C -% M% N

This profile specifies two output sources Stdout and R. The former outputs the log information to the console, and the latter is a rotation log file. The biggest file is 100KB. When a log file reaches the maximum size, log4j will automatically rename Example.log to eXample.log.1, and then rebuild a new Example.log file and rotate sequentially.

Use in web applications

In a web application, where should I configure the log4j? First of all, the log4j must complete the initialization before performing other code executions. Because servlet is immediately loaded immediately, in a web application, a special Servlet is usually used to complete the log4j configuration and ensure that this servlet is in other servlets in the Web.xml configuration. Here is an example, the code is as follows:

Package org.javaresearch.log4j;

Import java.io. *;

Import javax.servlet. *;

Import org.apache.log4j. *;

Public class log4jinit extends httpservlet {

Public void init () throws servletexception {

String prefix = GetServletContext (). GetRealPath ("/");

String file = getServletConfig (). GetInitParameter ("log4j-config-file");

/ / Read the log4j profile from the servlet parameter

IF (file! = NULL) {

PropertyConfigurator.configure (prefix file);

}

}

Public void doget (httpservletRequest Request, HttpservletResponse Response) THROWS

IOEXCEPTION, servletexception {}

Public Void Dopost (httpservletRequest Request, HttpservletResponse Response) THROWS

IOEXCEPTION, servletexception {}

}

log4jinit

org.javaresearch. log4j.log4jinit

Log4j-config-file

/properties/log4j.properties

1

转载请注明原文地址:https://www.9cbs.com/read-101026.html

New Post(0)