Log4j Configuration Document

xiaoxiao2021-03-06  57

Download

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

2.Log4j's Concise Manual (ENGLISH)

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

3. Installation

Log4j does not need to be installed, just need to compile the XML parser (ie, the Parser.jar file) in a JAXP (ie, Parser.jar file) in the classpath. You can use it in classpath. (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 Method for using the Java feature file as a configuration file:

4.2.1 Configuring Root Logger, its syntax is:

Log4j.rootlogger = [level], appendername, appendername, ... where Level is the priority of log records, divided into OFF, Fatal, Error, Warn, INFO, DEBUG, ALL, or your defined level. 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 = fully.qualified.name.of.appender.classlog4j.appender.appenderName.option1 = value1 ... log4j.appender.appenderName.option = valueN wherein, appender Log4j provided are the following: org.apache. Log4j.consoleAppender (console), org.apache.log4j.fileappender (file), org.apache.log4j.dailyrollingFileAppender (a log file is generated), org.apache.log4j.rollingFileAppender (file size is generated when the specified size is generated) A new file), org.apache.log4j.writerappender (send log information to any specified location in flow format)

4.2.2 Setting the format (layout) of log information, its syntax is:

log4j.appender.appenderName.layout = fully.qualified.name.of.layout.classlog4j.appender.appenderName.layout.option1 = value1 ... log4j.appender.appenderName.layout.option = valueN which, layout Log4j provided are the following Amount: org.apache.log4j.htmlLayout (layout in HTML table), org.apache.log4j.patternlayout (can be flexibly specified), org.apache.log4j.sImpleLayout (level and information string containing log information) ), Org.apache.log4j.ttccLayout (including information, thread, category, etc.) 4.3 use 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 a logger 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:

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

New Post(0)