Log4j Configuration Document

xiaoxiao2021-03-06  55

1 download

Http://jakarta.apache.org/log4j/docs/download.html

2Log4J's Concise Manual (English)

http://jakarta.apache.org/log4j/docs/manual.html

3 installation

Log4j does not need to be installed, just need to put 1 Log4j_home / dist / lib / log4j-version.jar, 2Log4j_home / dist / classes 3 a JAXP compiled XML parser (ie, Parser.jar file). Play in classpath you can use. (Log4j_home is the directory of log4j)

4 use

4.1 Introduction

LOG4J consists of three important components: the priority of log information, output destination of log information, and output format of log information. The priority of the log information is from high to Error, Warn, INFO, DEBUG, and is used to specify the importance of this log information. The output destination of log information specifies the log to print to the console or in the file; The format controls the display of log information.

4.2, use the Java feature file as a configuration file:

4.2.1 Configuring Root Logger, its syntax is:

Log4j.rootlogger = [level], appendername, appendername, ...

Among them, Level is the priority of logging, divided into OFF, Fatal, Error, Warn, Info, Debug, All, or level you defined. Log4J is recommended to use only four levels, priority from high to low, Error, Warn, Info, Debug. By the level defined here, you can control the switch to the corresponding level of log information in the application. For example, the INFO level is defined here, and all the DEBUG level log information in the application will not be printed. Appendername is where the specified log information is output to which place. You can specify multiple output destinations at the same time. Configure the log information output Destination Appender, its syntax is

Log4j.Appender.Appendername = flly.qualified.name.of.Appender.class

Log4j.appender.Appendername.Option1 = value1

...

Log4j.appender.Appendername.Option = VALUEN

Among them, the appender provided by Log4j has the following: org.apache.log4j.consoleAppender, org.apache.log4j.fileAppender (file), org.apache.log4j.dailyRollingFileAppender (a log file is generated every day), org .apache.log4j.rollingfileAppender Generate a new file when the specified size is specified, org.apache.log4j.writerappender (send log information to any specified place in the flow format)

4.2.2 Format (layout) of log information, 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

Among them, Layout provided by LOG4J has the following: org.apache.log4j.htmlLayout (layout in HTML table), org.apache.log4j.patternlayout (can be flexibly specified), org.apache.log4j.simplelayout ( The level and information string containing log information), org.apache.log4j.ttccLayout (including information, thread, category, etc.)

4.3 Using log4j in the code

4.3.1 Get a recorder

Using log4j, the first step is to obtain a logging device, which will be responsible for control log information. Its syntax is:

Public Static Logger getlogger (String name),

Get the recorder by the specified name, if necessary, create a new recorder for this name. Name generally takes the name of this class, such as:

Static logger logger = logger.getlogger (ServerWithlog4j.class.getname ());

4.3.2 Read the configuration file

After the log recorder is obtained, the second step will configure the log4j environment, whose syntax is: BasicConfigurator.configure (): Automatically quickly uses the default Log4j environment. PropertyConfigurator.configure (String configFileName): Read the configuration file written by using the characteristic file using Java. Domconfigurator.configure (String FileName): Read the configuration file in the XML form.

4.3.3 Inserting Record Information (Format Log Information)

When the top two necessary steps are performed, you can easily use different priority logging statements into any place you want to log log, whose syntax is as follows:

Logger.debug (Object Message);

Logger.info (Object Message);

Logger.warn (Object Message);

Logger. Error (Object Message);

5 example (source file and attribute file)

//Testlog4j.java

Package testlog;

Import java.io.ioException;

Import java.net.URL;

Import org.apache.log4j.logger;

Import org.apache.log4j.propertyConfigurator;

Public Class Testlog4j

{

Private static logger

Logger = logger.getlogger (Testlog.testlog4j.class.getname ());

Public testlog4j ()

{

}

Public static void main (String Argv [])

{

String resource = "/ testlog / Testlog4j.properties";

URL ConfigFileresource =

Testlog4j.class.getResource (resource); PropertyConfigurator.configure (configfileresource);

Logger.debug ("Hello, My Name Is Maggie Simpson.");

Logger.info ("info");

}

}

//Testlog4j.properties

# Set Logger and Level

Log4j.rootcategory = Debug, R

# 文件 文件 大 大 产生 到 指 指 文件 文件

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

# 输 日 文件 文件 Name

Log4j.appender.r.file = log.html

#file format

Log4j.Appender.r.Layout = org.apache.log4j.htmllayout

# Specify file size

Log4j.Appender.r.maxfilesize = 3kb

# Generate a new file, the original file name is log.html.1, ..., log.html.maxbackupindex

Log4j.appender.r.maxbackupindex = 2

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

New Post(0)