Log4j source code reading - Log4j initialization

xiaoxiao2021-03-06  39

Log4j source code reading - Log4j initialization

First, the initialization of log4j (in the static block in the Logmanager class).

1. Create a rootlogger with a debug grade, then create an instance of a Hierarchy class with rootlogger as a parameter.

2. The LoggerRepository interface and the renderersupport interface are implemented in the Hierarchy class. The LoggerRepository interface mainly provides some access methods to logger aggregation and the entire

The Log container supports the Threshold level. The Hierarchy class is actually the most important container class in the log4j, where all Logger instances are saved, and provides the appropriate way to maintain the logger's registration structure.

3. Create an instance of a defaultRepositorySelectorySelector class with hierarchy as a parameter. In order to provide a method for acquiring LoggerRepository.

4, the DefaultRepositorySelector class implements the RepositorySelector interface. RepositorySelector provides a method for getting loggerRepository.

5, start reading the default profile below. First try to read log4j.xml, if there is no longer reading log4j.properties. If you don't exist, give up the default configuration. Continue to run. If you are

There is no log4j in the program, and the latter run print is erroneous log.

Second, Log4J configuration file read (PropertyConfigurator, read the log4j.properties file)

1. Preparation PropertyConfigurator static method Configure () can accept three parameters: string, url, proties; configure () method generates one

The instance of PropertyConfigurator and calls the appropriate Doconfigure (XXX, LoggerRepository). Docofigure () method for the parameter with String and URL, after reading the configuration file

The Doconfigure (Properties, LoggerRepository method) is called.

2, DOCONFIGURE (PROPERTIES, LoggerRepository) Method DOCONFIGURE () method first sets the debug status of the Log4J class library itself, ie, reading the "log4j.debug" property;

Then set the Log4j's Hierarchy's Threshold property. These two properties are also two unique top-level properties in the configuration file. The following doconfigure () calls configurerootcategory ();

ConfigureLoggerFactory (); ParsecatsandRenderers () method configures rootlogger, loggerfacory, other Logger, and Renderer, respectively.

Third, ONFIGUROOTCATEGORY (Properties, LoggerRepository) Configuring rootlogger to read "log4j.rootlogger" properties, synchronous rootlogger call

The parsecatetory () method begins to configure rootlogger.

1, Parsecategory (Properties, Logger, String Key, String Loggername, String Value) Configuring Logger Value The first comma is logger level,

In addition to rootlogger, other loggers can also be set to "INTERNAL", if the Logger is set to Level, set the level, otherwise Level is set to empty. Before starting the Appender of the Logger

Delete and close all the appenders in the Logger. Removing this Logger associated here, it is understood that all the Appenders involved is all closed, and I don't know what mystery. Closing the associated appender also makes the log4j's multiple configurations, it is easy to go wrong. The first comma in Value is later divided by a comma, after reading a valid appendername, repeat call

PARSEAPPENDER () Configure the appender. After configuring it, the configuration is hung in the Logger.

2, PARSEAPPENDER (Properties, String Appendername) Configure the appender first try to read registry, see if this apioner has been equipped during this configuration.

Set it, if there is already a configuration Appender, return directly to the configured appender. If this is the first time to configure this appendername, read the log4j.Appender. [AppenderName] property,

Generate the corresponding Appender instance. And set this appender name for [AppenderName]. The judgment claimed is an instance of OptionHandler, and determines whether the appender needs

Layout, if you need to read "log4j.Appender. [Appendername] .Layout 'attribute, generate the corresponding Layout instance and load it into the appender. Then call

PropertySetter.SetProperties () Sets the properties of Layout. Then call PropertySetter.SetProperties () to set the properties of the appender (set up the process can be referred to

The setting of the Layout property). Finally, the configured appender is added to Registry.

3, the PropertySetter class uses Introspection and Reflection to set up the corresponding set of properties. (The class to be set must have the corresponding setter, getter method, and only one in the method.

Parameters (defaults to string)). Static Method SetProperties (Object, Properties, String Prefix) First use Object to generate an instance of a PropertySetter,

A SETPROPERTIES (Properties, prefix) method. SetProperties (Properties, String Prefix) is traversed in the Properties to start [prefix] and

[prefix] is not directly with the point number "." attribute. When the set Object is an instance of the apnder, "[prefix.layout]" attribute (because this property is already

The parseAppender () method is configured). Then call the SetProperty (key, value) method to set the properties. SetProperty (String Name, String Value) First Utilization

The province acquires PropertyDescriptor with the Name property, and then calls setProperty (PropertyDescriptor, name, value) using the reflection to really set the name property of the Object. most

If the object is set, the actiVateOptions () method is called, and the settings set the settings, such as checking the files, and then applied to the appropriate properties.

Fourth, configureLoggerFactory (Properties) Configure LoggerFactory, LoggerFactory affects the creation of later Logger.

1. Read the "log4j.loggerFactory property, if the property is a valid class name, and make the LoggerFactory subclass, create an instance given to the class and saves a protection variable of the PropertyConfigurator class LoggerFactory.

2. Call the PropertySetter.SetProperties () method to "log4j.factory." Is prefixed to the LoggerFactory setting property (the setting process can refer to the Layout property "

setting).

5. Parsecatsandreerers (Properties, LoggerRepository) Configuring normal Logger and Renderer.

1. Configuration of ordinary logger Traversed Properties, read all properties starting with "log4j.category." And "log4j.logger.", Intercepting the logger name call

LoggerRepository.getLogger (LoggerName, LoggerFactory) Generates a new Logger. Regarding the newness of Logger, discuss it separately. Synchronized new Logger, call

Parsecategory () Method Configure the properties of the Logger (see Configuration of the ROOTLogger in front). Then call the parseadditivityforllogger () method to set the Logger's inheritance properties.

PARSEADDITIVITYFORLOGER (Properties, Logger, String Logger), first read "Log4j.Additivity. [LoggerName]" attribute, and call

Logger.SetAdditivity () Method Sets the inheritance properties of the Logger.

2, Renderer's configuration traversed Properties, read all properties starting with "log4j.renderer.", Intercepting the renderer name, if LoggerRepository is

An instance of RENDERSUPPORT calls the renderemap.addrenderer () method Add Renderer.

3, RendereMap.Addrender (renderersupport, string renderedclassname, String RenderingClassName) Add interpreter generated

The renderingClassName class instance, and then call renderer (renderedClass, Rendere) to add RENDERER to the Logger container.

6. Finally, will save the configured appender's registry empty.

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

New Post(0)