logback entry

Recently, I want to promote the project team to use logback on new products/new projects, so I plan to learn about logback
1. The official manual http://logback.qos.ch/manual/, the introduction is very detailed.
2. Basically, you only need to introduce the following three jars to meet the log features similar to log4j.
logback-classic-1.1.8.jar
logback-core-1.1.8.jar
slf4j-api-1.7.22.jar

3. Read the source code for preliminary sorting

4. The next step is to organize a sample of the configuration file by reading the official manual + reading the source code
//TODO

5. Difference analysis between logback and log4j callAppender methods

    a. The callAppender method of log4j uses the synchronized keyword to modify the logger obtained in the for loop; logback does not use the synchronized
    keyword. b. Both log4j and logback's addAppender/removeAppender methods are modified with the synchronized keyword; logback's detachAppender does not use the synchronized keyword. The word modifier
    c.log4j uses Vector to store appenders; logback uses CopyOnWriteArrayList to store appenders
    . If d.log4j does not use synchronized in the callAppender method, other threads will continue to call addAppender/removeAppender and other methods under concurrent conditions to change the appenders stored in Vector. Since the appender is traversed using for(int i...), it will lead to out-of-bounds (the array becomes smaller) or failure to access the subsequently added appender
    e.log4j. Due to the synchronized modification of the Logger, when multiple threads concurrently access the same When there is a logger, there will be a situation of waiting for the lock, and there is a performance risk
    . f.logback uses CopyOnWriteArrayList to store the appender, and the appender is traversed using foreach, which can avoid out-of-bounds (the array becomes smaller) and the appender that fails to locate the subsequent additional results.
    The iterator of g.CopyOnWriteArrayList returns the COWIterator object. Each hasNext uses the length of the array object passed in during construction (the array will be changed after the execution result of addIfAbsent and remove) is judged by the cursor, so it can avoid the risk of crossing the boundary. It can also avoid not accessing the newly added successfully element (appender) in time

ps:

       a.log4j creates a Hierarchy instance through LogManager during initialization, all loggers are stored in the HashTable of Hierarchy, and builds a parent-child relationship according to the package path hierarchy
       b.logback obtains a single instance through StaticLoggerBinder initialization, and uses its defaultLoggerContext property To point to the LoggerContext instance, all loggers are stored in the ConcurrentHashMap of LoggerContext, and the parent-child relationship is constructed according to the package path hierarchy

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326710555&siteId=291194637