Configuration of Log4j

xiaoxiao2021-03-05  27

The configuration of the log4j environment is the configuration of the root logger, including which level of the root logger is set (Level); to add which appenders, and so on. These can be implicitly completed by setting the method of setting the system properties, or the XXXConfigurator.configure () method can be called in the program to explicitly complete.

Log4j can be configured in two ways

A) Program configuration

The BasicConfigurator.configure () method configures the Log4j environment using the simplest method. Note: The so-called configuration log4j environment is to configure root logger because all other Loggers are offspring of root logger, so they will inherit the nature of root logger.

The task of BasicConfigurator.configure () is:

Create a PatternLayout object with default Pattern P: patternlayout p = new patternLayout ("% - 4R [% t]% - 5P% C% X -% M% N"); Create consOLLAPPENDER object A with p, the target is System.out, standard output device: ConsoleAppender a = new ConsoleAppender (p, ConsoleAppender.SYSTEM_OUT); ConsoleAppender p increases as a root logger: rootLogger.addAppender (p); the log level of the root logger to DEBUG level: rootLogger.setLevel (Level.DEBUG );

B) file configuration

B1) configuration file in XML format

The tree structure of the Log4j's XML configuration file is as follows, making it a view that the following figure only shows the commonly used portions. :.

XML Declaration and DTD | LOG4J: Configuration | - Appender (Name, Class) | | | - Param (Name, Value) | - Layout (Class) | | | - Param (Name, Value) - Logger (Name, Additive) | | | - Level (Class, Value) | | | | | - Param (Name, Value) | - Appender-Ref (Ref) - ROOT | - Param (Name, Class) - Level | | | - Param (Name, Value) - Appender-Ref (Ref)

Creating a consolerappener object consolerappender constructor does not accept other parameters. :.

... ... ... < / appender> ... ... Create a FileAppender object to pass two parameters for the FileAppender class: file represents the log file name; append means that if the file already exists, if the log is added to the end of the file It may take the value "true" and "false". :.

... ... ... ... ... ...

Creating a ROLLINGFILEAPPENDER object In addition to file and append, two parameters can be passed for the construction method of the ROLLINGFILEAPPENDER class: the number of MaxBackupIndex backup log files (default is 1); MaxFileSize indicates the maximum number of bytes allowed by log files (default is 10m ). :.

... ... ... ... ... ... ...

Creating a PatternLayout Object You can deliver parameters ConversionPattern. :.

... ... ... ... a complete example is as follows:

Log4j.xml

You can read the configuration information file into the Java program with the following method: Domconfigurator.configure ("classes / log4j.xml);

B1) Profile in the form of text file

one example:

Log4j.properties

# Loggerlog4j.rootlogger = Debug, appender1, appender2

# Appender1log4j.Appender.Appender1 = org.apache.log4j.fileAppenderLog4j.Appender.Appender1.file = gasturbine.log1.txt

# Appender2log4j.appender.appender2 = org.apache.log4j.RollingFileAppenderlog4j.appender.appender2.File = gasturbine.log2.txtlog4j.appender.appender2.MaxFileSize = 1024KBlog4j.appender.appender2.MaxBackupIndex = 5

# LAYOUT1LOG4J.Appender.Appender1.Layout = org.apache.log4j.patternLayoutLog4j.Appender.Appender1.Layout.conversionPattern = [% D]% T% C% -5p -% M% N

# LAYOUT2LOG4J.Appender.Appender2.Layout = org.apache.log4j.patternLayoutLog4j.Appender.Appender2.Layout.conversionPattern = [% D]% T% C% -5P -% M% N

#define the level of packagelog4j.logger.gasturbine.Model.component.compmodel.volume = debug

It's very tired with the XML file function, no longer nonsense.

Pay attention to loading: PropertyConfigurator.configure ("Classes / Log4j.properties);

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

New Post(0)