Log4j source code reading - Logger creation

xiaoxiao2021-03-06  40

I. Log 4j's hierarchy log4j is organized in a two-way tree structure, but log4j is not organized with the structure of the Logger using the usual leaf nodes and branches. The structure of the two tissue trees of the virtual node and the Logger node are used in Log4j. The logger node that is really created by the user is represented by a real logger node, and the ancestors of the logger node may not be true Logger, in order to improve the efficiency of log4j, here is used here. ProvisionNode inherits to the Vector and provides a construction method for accepting a Logger. It is just a station in the hierarchy of the log4j. Note: ProvisionNode saves instances of all sub-loggers. If a Logger's parent Logger is a virtual node, his parent logger is rootlogger. Second, the role of the hierarchical structure in the log4j, so that the node determined in each level has only one logger instance, reducing the memory consumption of the Logger. 2. Make the Logger of the hierarchy can be the level of its ancestor. Without setting the level, the level Logger is set, and the Logger can be turned off directly from the ancestor logger at one time (the grade of the ancestor logger is OFF) one-time logger. Implementation: From ourselves to the ancestral approach to the ancestor method, until the access is not empty. If the parent Logger is a virtual node, his parent logger is rootlogger. 3. Make the Logger of the hierarchy can be the Appender of the grass. Without each logger sets the appender, and in the case where the underlying logger does not set Appender, you can replace the Logger's appender from the ancestor logger at one time. Implementation: From ourselves to the ancestral approach to Appenders. If the inheritance property is false, then the loop is jumped directly after calling all the Appenders. Third, Log4j's creation 1, all Logger's creation will be sent to the getLogger () method of the interface LoggerRepository. The implementation of this interface in log4j is hierarchy. The creation process provided in Hierarchy is as follows: 2, it is judged whether or not this name has a Logger that already exists in the Logger container. There may be three cases 1), there is no existence, the factory method has created a new logger, set the Logger's Repository property, put the logger in the Logger container, call the UpdateParents () method to update the Logger hierarchy. 2), exist and type Logger, directly returns the Logger. 3), exist, type of ProvisionNode, set the Logger's Repository property, put the logger in the Logger container, call the UpdateChildren () method and updateParents () method to update the Logger hierarchy. 3. UpdateParents (Logger) method. Update the hierarchy of the parent Logger.

Attempting to get a ancestor logger, there may be the following cases 1), if the parent logger exists, and the type is Logger, set to the parent Logger of this Logger. Jump out the loop. 2) If the parent logger exists, and the type is provisionNode, the Logger is added to the provisionNode and then proceeds. 3) If the parent Logger does not exist, create the corresponding provisionNode, add this Logger to the ProvisionNode and continue the loop. Note: It is very important to continue the cycle at that time, this is an important way to maintain the logger level. Such a result is "Example of ProvisionNode Save all sub-logger." 4, UpdateChildren (ProvisionNode, Logger) method. Traversing all Logger in ProvisionNode, setting all the parent Logger that does not work properly on your true parent Logger to create Logger to replace ProvisionNode. Fourth, LOG4J, LOG level, 1, if a Logger's Logger type is Logger: The parent Logger of this logger directly points to the father's LOGGER. 2, if a Logger's Logger type is provisionNode: The parent of this Logger point to rootlogger. 3, if a Logger's parent logger type is provisionNode, but there is a logger in the ancestor logger. The parent of this Logger points to a nearest Logger in all ancestors. V. The printing process of logs in log4j is parsed by the INFO () method. Use consOLLAPPENDER and PATTERNLAYOUT. 1. Firstly, the ISDISabled () method of the LoggerRepository interface is called, and the LOG level in this level is determined by the settings () method in the INFO () method) can be printed in the INFO () method. If you can't exit it directly. 2. Call the getEffectiveElevel () method traversed the grade structure of the logger to find out the level of the Logger. And the LOG level in this level (in the info () method is Level.info), it is determined whether the Logger prints the LOG of this level. If you can print, call the ForcedLog () method. The forcedLog () method generates a logGingEvent instance by incoming parameters and then calls the Callappenders () method. 3, Callappenders (LoggingEvent) method Through the AppenderattachableImpl class, the CallatTachableImpl class, first over all of the Appenders in the Logger and call the appropriate DoaPpend () method. If the Logger closes the inheritance switch, exit the loop directly, otherwise traverses all ancestors' appenders. Finally, it is judged that the number of writings is logged. If you are equal to 0, you can print without an Appender error history. 4, the AppendrattachableImlp class implements the Appendrattachable interface and provides an Appender aggregation, and the approach to the aggregation method appendlooponappenders (LogGingEvent). By calling the method, you can call the DOAPPEND method of all Appenders in the argument to call the DOAPPEND method in the argument.

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

New Post(0)