log4j2使用手册@zhangsf(3)

2020-04-14 16:45

Event Level LoggerConfig Level TRACE DEBUG INFO WARN ERROR FATAL OFF ALL YES YES YES YES YES YES NO 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 7. Filter

Log4j2提供了 Filters, 可以 应用于 控制权传递到 LoggerConfig之前,控制权传递到LoggerConfig之后 但是在调用 Appenders之前,控制权在传递到 LoggerConfig之后,但是在调用一个指定的 Appender 之前。类似于防火墙的处理方式,每个Filters可以返回三个结果的其中之一。 Accept, Deny Neutral 。 Accept意味着不会再调用其他Filters 了,LogEvent 将被执行。

Deny 意味着立即忽略这个LogEvent,并将这个LogEvent 的控制权交还给它调用者。

Neutral 指LogEvent将传递给其他Filters ,如果没有别的Filters 了,那么这个LogEvent将被执行。

尽管一个LogEvent 可能被一个Filter接收,但是这个LogEvent仍然没有被日志记录下来,发生这种情况的场景可能是LogEvent被pre-LoggerConfig Filter接收了,但是却被LoggerConfig拒绝了,或者被所有的Appenders拒绝了。

8. Appender

由logger的不同来决定一个logging request是被禁用还是启用只是log4j2的情景之一。log4j2还允许将logging request中log信息打印到不同的目的地中。在log4j2的世界里,不同的输出位置被称为Appender。目前,Appender可以是console、文件、远程socket服务器、Apache Flume、JMS以及远程 UNIX 系统日志守护进程,数据库。一个Logger可以绑定多个不同的Appender。

可以通过调用当前Configuration的addLoggerAppender 方法为Logger增加一个Appender 。如果不存在一个与Logger名称相对应的LoggerConfig,那么相应的LoggerConfig将被创建,并且新增加的Appender将被添加到此新建的LoggerConfig中。然后,所有的Loggers将会被通知更新自己的LoggerConfig引用(PS:一个Logger的LoggerConfig引用是根据名称的匹配长度来决定的,当新的LoggerConfig被创建后,会引发一轮配对洗牌)。

某个logger 中被启用的 logging request 将被转发到该LoggerLoggerConfig 相关联的的

所有Appenders上,并且还会被转发到LoggerConfig的父级的Appenders上。 换句话说,Appenders将从LoggerConfig的层次结构中获得继承。

例如,对LoggerConfig B来说,它的父级为A,A的父级为root。如果在root中定义了一个Appender为console,那么所有启用了的logging request都会在console中打印出来。另外,如果LoggerConfig A定义了一个文件作为Appender,那么使用LoggerConfig A和LoggerConfig B的logger 的logging request都会在该文件中打印,并且同时在console中打印。

如果想避免这种遗传效应的话,可以在configuration文件中做如下设置:additivity=\这样,就可以关闭Appender的遗传效应了。

具体解释如下 :

Appender Additivity The output of a log statement of Logger L will go to all the Appenders in the LoggerConfig associated with L and the ancestors of that LoggerConfig. This is the meaning of the term \However, if an ancestor of the LoggerConfig associated with Logger L, say P, has the additivity flag set to false, then L's output will be directed to all the appenders in L's LoggerConfig and it's ancestors up to and including P but not the Appenders in any of the ancestors of P. Loggers have their additivity flag set to true by default.

上面例子的详细表格解释如下:

Logger Name Added Appenders Additivity Flag Output Targets Comment root A1 not applicable A1 The root logger has no parent so additivity does not apply to it. x A-x1, A-x2 true A1, A-x1, A-x2 Appenders of \x.y none true A1, A-x1, A-x2 Appenders of \It would not be typical to configure a Logger with no Appenders. x.y.z A-xyz1 true A1, A-x1, A-x2, A-xyz1 Appenders in \and root. security A-sec false A-sec No appender accumulation since the additivity flag is set to false. security.access none true A-sec Only appenders of \additivity flag in \set to false.

9. Layout

通常,用户不止希望能定义log输出的位置,还希望可以定义输出的格式。这就可以通过将

Appender与一个layout相关联来实现。Log4j中的 一个标准定义PatternLayout,就允许用户使用一种类似C语言printf函数的打印格式,

如\格式在真实环境下会打印类似如下的信息: 176 [main] INFO org.foo.Bar - Located nearest gas station.

第一个字段是启动的毫秒数,第二个字段是日志请求的线程号, 第三个字段是日志请求级别,第四个是与日志请求相关联的日志名称, 在“-” 之后的内容就是日志内容。

Log4j有各种不同的 Layouts, 例如 : JSON, XML, HTML, 和Syslog (including the new RFC 5424 version). 其他一些appenders例如database connectors会使用指定的字段替代特定的文本布局。

同样重要的是,要使得log4j的日志消息内容符合用户指定的标准。,例如:在你的工程中,你需要一个Oranges类型的日志对象,你就可以创建一个OrangeMessage,可以接受Orange实例并且传递到log4j,Orange对象就会被格式化为一个适当的字节数组。

10. StrSubstitutor and StrLookup

StrSubstitutor 类与 StrLookup接口是来自Apache Commons Lang, 并且被修改为支持 LogEvents 的运算。此外 来自Apache Commons Configuration的 Interpolator类允许StrSubstitutor 运算来着多个StrLookups 的变量,它也被修改支持LogEvents的运算。 Log4j2提供了一种机制,可以使得configuration引用 来自System Properties, the configuration file, the ThreadContext Map, StructuredData中的变量。


log4j2使用手册@zhangsf(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:广州市三旧改造政策解读

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: