Log4j 2体系结构

Log4j 2体系结构

下图是Log4j官网给出的类图:
log4j2-architecture2019-12-27-10-49-4html

咱们先依次看一下每一个类的定义是什么。java

1. 前置知识

LoggerContext

// todo 不太理解数据库

Configuration

Configuration类对应的是正在使用的配置文件,例如log4j2.xml。它包含了全部可用的AppenderLoggerConfig、以及Filter的定义。apache

During reconfiguration two Configuration objects will exist. Once all Loggers have been redirected to the new Configuration, the old Configuration will be stopped and discarded.

在从新配置时(即配置文件有改动时),可能会并存两个 Configuration对象。一旦全部的 Logger都被重定向到新的配置,旧配置对应的 Configuration对象就会被废弃。

Logger

Logger对象经过LogManager.getLogger方式得到,通常每一个类都有各自的Logger对象,名称与该类的全量名一致。Logger应该只是一个接口,自己不执行任何直接操做。,拥有名字并与一个LoggerConfig相关联。服务器

The Logger itself performs no direct actions. It simply has a name and is associated with a LoggerConfig.

Logger的行为由与其关联的LoggerConfig控制,当其与其余的LoggerConfig相关联时,它的行为也会做相应的改变。app

LoggerConfig

LoggerConfig对象对应着配置文件(例如log4j2.xml)中的<Logger>。它包含了一系列Filter定义以及对一系列Appender的引用。socket

LoggerConfig objects are created when Loggers are declared in the logging configuration.

Log4j 1.x与Log4j 2.x的不一样在于,日志的层次结构发生了变化。在1.x中,日志的层次结构是经过Logger之间的关系维护的,而在2.x中,是用LoggerConfig对象之间的关系维护的。this

In Log4j 1.x the Logger Hierarchy was maintained through a relationship between Loggers. In Log4j 2 this relationship no longer exists. Instead, the hierarchy is maintained in the relationship between LoggerConfig objects.

命名原则

在Log4j 2.x中,LoggerLoggerConfig会根据名称进行关联,并且它们的命名都知足以下的层次关系:
名为javaLoggerConfig是名为java.utilLoggerConfig配置,是名为java.util.VectorLoggerConfig祖先配置。
反过来,名为java.util.VectorLoggerConfig是名为java.utilLoggerConfig配置,是名为javaLoggerConfig子孙配置。spa

A LoggerConfig is said to be an ancestor of another LoggerConfig if its name followed by a dot is a prefix of the descendant logger name. A LoggerConfig is said to be a parent of a child LoggerConfig if there are no ancestors between itself and the descendant LoggerConfig.

Root LoggerConfig在整个LoggerConfig体系的最顶层,它没有任何祖先配置。能够经过LogManager.getLogger(LogManager.ROOT_LOGGER_NAME)LogManager.getRootLogger()的方式得到与Root LoggerConfig相关联的Logger日志

日志等级

在Log4j 2.x中,每一个LoggerConfig都会被分配一个Log level。Log4j 2.x内建支持的Log level为:TRACE < DEBUG < INFO < WARN < ERROR < FATAL。除了这些内建等级,Log4j 2.x也容许用户去自定义等级(不过官方并不推荐这么作)。

除了等级机制,可使用Marker机制来实现更细粒度的控制,更多信息请参考[]()。

接下来,咱们用下面5组实例来介绍一下Log4j 2.x中的Level Inheritance(等级继承)机制。

Logger Name Assigned LoggerConfig LoggerConfig Level Logger Level
root root DEBUG DEBUG
X root DEBUG DEBUG
X.Y root DEBUG DEBUG
X.Y.Z root DEBUG DEBUG
上表中,只有名为root的 Logger被赋予了名为root的 LoggerConfigLoggerConfig的等级为DEBUG,因此名为root的 Logger的等级也为DEBUG。其它的 Logger都没有特别分配 LoggerConfig,所以会继承它们的祖先配置root。
Logger Name Assigned LoggerConfig LoggerConfig Level Logger Level
root root DEBUG DEBUG
X X ERROR ERROR
X.Y X.Y INFO INFO
X.Y.Z X.Y.Z WARN WARN
上表中,每一个 Logger都与各自同名的 LoggerCongfig所关联,所以它们的等级都与相关联的 LoggerConfig的等级保持一致。
Logger Name Assigned LoggerConfig LoggerConfig Level Logger Level
root root DEBUG DEBUG
X X ERROR ERROR
X.Y X ERROR ERROR
X.Y.Z X.Y.Z WARN WARN
上表中,惟独没有为名为X.Y的 Logger分配同名的 LoggerConfig,所以它将与名为X的 LoggerConfig相关联(父配置,而非是祖先配置root)。
Logger Name Assigned LoggerConfig LoggerConfig Level Logger Level
root root DEBUG DEBUG
X X ERROR ERROR
X.Y X.Y INFO INFO
X.YZ X ERROR ERROR
从上表中能够看出,层次结构是以 .做为分割的,而不是最长的名称匹配长度。因此X.YZ的父配置为X,而不是X.Y。
Logger Name Assigned LoggerConfig LoggerConfig Level Logger Level
root root DEBUG DEBUG
X X ERROR ERROR
X.Y X.Y ERROR
X.Y.Z X.Y ERROR
上表中,虽然名为X.Y的 Logger与其同名 LoggerCongfig相关联,但未被赋予等级,将继承名为X的 LoggerConfig的等级ERROR。

下标展现了根据等级的过滤规则:

Event Level LoggerConfig Level
- TRACE DEBUG INFO WARN ERROR FATAL OFF
ALL YES YES YES YES YES YES YES
TRACE YES NO NO NO NO NO NO
DEBUG YES YES NO NO NO NO NO
INFO YES YES YES NO NO NO NO
WARN YES YES YES YES NO NO NO
ERROR YES YES YES YES YES NO NO
FATAL YES YES YES YES YES YES NO
OFF NO NO NO NO NO NO NO

Appender

Appender指日志输出的目的地。Log4j 2.x支持控制台、文件、远程socket服务器、Apache Flume、JMS、远程Unix Syslog守护进程和数据库API等做为日志输出的目的地。关于这些类型的详细信息,请参考Appenders

In log4j speak, an output destination is called an Appender.

Layout

Filter

StrSubsitutorStrLookup

相关文章
相关标签/搜索