Complete log4j configuration method

Source: Internet
Author: User
Tags syslog

With the emphasis on reusable component development, in addition to developing a reusable log operation class from start to end, Apache provides us with a powerful log operation package-log4j.

Log4j is an open-source project of Apache. By using log4j, we can control the log information delivery destination, including the console, files, Gui components, and even the interface server, NT event recorder, and Unix Syslog daemon; 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. The most interesting thing is that these can be configured flexibly through a configuration file, without the need to modify the application code.

In addition, through interfaces of other log4j languages, you can ,. net and PL/SQL programs use log4j. Its syntax and usage are the same as those in Java programs, so that the multi-language distributed system can obtain a unified and consistent log component module. Moreover, by using various third-party extensions, you can easily integrate log4j into J2EE, Jini, and even SNMP applications. The log4j version described in this article is 1.2.8. The main application platform for flexible configuration through a configuration file is tomcat4.

1.3. Log4j configuration.

First download a log4j component from Jakarta. Copy the jakarta-log4j-1.2.8 file under log4j-1.2.8.jar/Dist/lib to the directory specified by classpath! It can be the common/lib directory of Tomcat, or the lib directory under the application that you need to use log4j.

1.4 Add the following code to the Web. xml file in the application directory

<Servlet>
<Servlet-Name> log4j </servlet-Name>
<Servlet-class> com. Apache. Jakarta. log4j. log4jinit </servlet-class>
<Init-param>
<Param-Name> log4j </param-Name>
<Param-value>/WEB-INF/log4j. properties </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>

This code indicates loading the class file com. Apache. Jakarta. log4j. log4jinit named log4jinit. Class at Tomcat startup. The source code of log4jinit. Class is as follows:

Package com. Apache. Jakarta. log4j;
Import org. Apache. log4j. propertyconfigurator;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Public class log4jinit extends httpservlet {
Public void Init (){
String prefix = getservletcontext (). getrealpath ("/";
String file = getinitparameter ("log4j ";
// If the log4j-init-file is not set, then no point in trying
System. Out. println ("...... log4j start ";
If (file! = NULL ){
Propertyconfigurator. Configure (prefix + file );
}
}
Public void doget (httpservletrequest req, httpservletresponse res ){
}
}
This code is very simple, it can be seen that in the loading process, the program will read the/WEB-INF/log4j. properties File
This file is the focus of this article, that is, the log4j configuration file.

# Set root logger level to debug and its only appender to A1
# Log4j has five levels of logger
# Fatal 0
# Error 3
# Warn 4
# INFO 6
# Debug 7
# Configure the root logger with the Syntax:
# Log4j. rootlogger = [level], appendername, appendername ,...
Log4j. rootlogger = info, A1, R
# This sentence indicates that all logs are output.
# If it is log4j. rootlogger = warn, it means only warn, error, fatal
# Output. debug and info will be blocked.
# A1 is set to be a consoleappender.
# The appender in log4j has several layers, such as the console, files, Gui components, and even an interface server, NT event recorder, and Unix Syslog daemon.
# Leleappender output to the console
Log4j. appender. A1 = org. Apache. log4j. leleappender
# Output layout used by a1. log4j provides four layout S: org. Apache. log4j. htmllayout (layout in HTML form)
# Org. Apche. log4j. patternlayout (you can flexibly specify the layout mode ),
# Org. Apache. log4j. simplelayout (including the log information level and information string ),
# Org. Apache. log4j. ttcclayout (including the log generation time, thread, category, and other information)
Log4j. appender. a1.layout = org. Apache. log4j. patternlayout
# Flexibly define the output format. For details, see log4j javadoc org. Apache. log4j. patternlayout.
# D time ....
Log4j. appender. a1.layout. conversionpattern = %-d {yyyy-mm-dd hh: mm: SS} [% C]-[% P] % m % N
# R extension output to file rollingfileappender, which can provide a log backup function.
Log4j. appender. r = org. Apache. log4j. rollingfileappender
# Log File Name
Log4j. appender. R. File = log4j. Log
# Log File Size
Log4j. appender. R. maxfilesize = 100kb
# Saving a backup file
Log4j. appender. R. maxbackupindex = 1
Log4j. appender. R. layout = org. Apache. log4j. ttcclayout
# Log4j. appender. R. layout. conversionpattern = %-d {yyyy-mm-dd hh: mm: SS} [% C]-[% P] % m % N
The configuration is similar here. If you want to learn more about the configuration file details, you can view docs. Also, at the end of the article, we provide some major syntaxes in the configuration file. Next let's take a look at how to use log4j in a program.
1.4 Use of log4j.
When using log4j, the first step is to obtain the logger, which controls the log information. Its syntax is:
Public static logger getlogger (string name ),
This class must be imported before use
Import org. Apache. log4j. Logger;
Name is generally the name of a class file, as follows:
Static logger = logger. getlogger ("". Class. getname ());
You can easily insert a log record statement with different priorities to any location where you want to record the log. The syntax is as follows:
Logger. debug (Object message );
Logger.info (Object message );
Logger. Warn (Object message );
Logger. Error (Object message );
Why should we classify them here? When writing a program, we add a lot of logger.info (); Information to debug the program. Of course, after the program debugging is completed, we don't need the output information. What should we do? The previous practice was to delete logger.info in each program, but this is unrealistic. If the program is not big enough, but if there are many programs, it will be annoying to do these things. However, because log4j is classified, when we do not need to output the log.info () used for such debugging, we can increase the output level, for example, to warn or error level, in this way, the info level and below won't be output, is it very convenient?
In addition to this method, log4j also has other usage aspects. You do not need a configuration file to define the input level, level, and other information in the program. If you want to know how to use this method, for more information, see.
1.5. Note:
The following are some important syntaxes of the configuration file:
Define the configuration file
In fact, you can configure the log4j environment in code instead of using the configuration file. However, using the configuration file will make your application more flexible.
Log4j supports two configuration file formats: XML and Java (Key = value ). The following describes how to use a Java feature file as a configuration file:
Configure the root logger with the Syntax:
Log4j. rootlogger = [level], appendername, appendername ,...
Level is the log record priority, which can be off, fatal, error, warn, info, debug, all, or the level you define. We recommend that you use only four levels of log4j. The priority ranges from high to low: error, warn, info, and debug. By defining the level here, you can control the switch to the corresponding level of log information in the application. For example, if the info level is defined here, all debug-level logs in the application will not be printed.
Appendername specifies where the log information is output. You can specify multiple output destinations at the same time.
Configure the appender of the log output destination. Its syntax is
Log4j. appender. appendername = fully. Qualified. Name. Of. appender. Class
Log4j. appender. appendername. option1 = value1
...
Log4j. appender. appendername. Option = valuen
Log4j provides the following types of appender:
Org. Apache. log4j. leleappender (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 stream format)
Configure the log information format (layout). Its syntax is:
Log4j. appender. appendername. layout = fully. Qualified. Name. Of. layout. Class
Log4j. appender. appendername. layout. option1 = value1
...
Log4j. appender. appendername. layout. Option = valuen
Log4j provides the following layout types:
Org. Apache. log4j. htmllayout (in the form of HTML tables ),
Org. Apache. log4j. patternlayout (you can flexibly specify the layout mode ),
Org. Apache. log4j. simplelayout (including the log information level and information string ),
Org. Apache. log4j. ttcclayout (including the log generation time, thread, category, and so on)

 

Transfer from Internet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.