2.
4.
5.
9.
12.
13. 14. 15. 16.
在设置 additivity=\后, 日志事件不会再传递到父Logger。
3. 自动重载
当Log4j2的配置是从文件中加载时, log4j2可以监测文件的变化,并且自动重载这些配置,当配置
monitorInterval 属性,并且值不为0的时候,Log4j2就会在下次时间间隔重载配置。下面的配置 就会每30
秒检查一次文件的变化,如果发生变化,配置文件就会被重载, 最小时间间隔为 5秒 :
1. 2.
4.
4. 使用 Chainsaw 智能处理日志文件
Chainsaw是一个Log4J包的 GUI日志查看器和过滤器。它使用SocketAppender听从LoggingEvent对象发送然后在一个图表中把它们显示出来。在优先级,线程名,类别名或者报文的基础上,这些事件都可以被过虑。它也可以写入事件记载到一个文件上。
Log4j2为 所有 file-based appenders 和 socket-based appenders 提供了“广播”功能。例如:对于 file-based appenders,广播中就包括日志文件的位置和格式,Chainsaw 或者其他扩展系统可以发现这些广播,并且使用广播中的信息对日志文件进行智能处理。
广播的曝光机制,以及广播的形式,是通过Advertiser来实现的。一个外部如果想要处理一个特定的Advertiser实现,就必须了解广播的配置与格式。例如,一个数据库的Advertiser 可能会将log详细配置存储在数据库的表中,然后扩展系统通过读取数据库的表去发现日志的位置与格式。
Log4j2提供了一个 Advertiser 的实现,“multicastdns,”是通过使用 http://jmdns.sourceforge.net的IP组播 来实现的 。
Chainsaw 会自动发现 log4j multicastdns 产生的广播,然后在Chainsaw's的Zeroconf tab中展示,然后开始跟踪和分析广播中的日志文件,目前,Chainsaw 只支持FileAppender 的广播。
广播在appender 中的配置:
1.在应用的classpath上增加 JmDns library ,http://jmdns.sourceforge.net 2.将configuration 元素的advertiser 属性设置为multicastdns 。 3.将appender 元素上的advertise 属性设置为 true
4.如果是 FileAppender-based 类型的配置,在appender 中将advertiseURI设置为合适的URI。
FileAppender-based 类型的配置要求在appender 上指定advertiseURI 属性,advertiseURI属性提供了Chainsaw如何访问该文件信息。例如:Chainsaw 可以通过ssh/sftp(使用Commons VFS (http://commons.apache.org/proper/commons-vfs/) sftp:// URI )远程访问,或者使用http:// URI通过 web server 来访问该文件,或者使用 file:// URI 来让本地运行的Chainsaw 实例来访问该文件
下面是一个启用广播的例子,可以通过 本地运行的 Chainsaw自动访问 。
Please note, you must add the JmDns library from http://jmdns.sourceforge.net to your application classpath in order to advertise with the 'multicastdns' advertiser
1. 2.
4. 5.
6. ut.log\advertise=\> 7. ... 8. 9. 5. Configuration 语法 本节只介绍 XML的语法,不在介绍 JSON, Properties, YAML的语法,需要的可以参考: http://logging.apache.org/log4j/2.x/manual/configuration.html#Loggers 在log4jxml配置中, configuration 元素属性如下 : Attribute Name advertiser Description (Optional) The Advertiser plugin name which will be used to advertise individual FileAppender or SocketAppender configurations. The only Advertiser plugin provided is 'multicastdns\Either \The minimum amount of time, in seconds, that must elapse before the file configuration is checked for changes. The name of the configuration. A comma separated list of package names to search for plugins. Plugins are only loaded once per classloader so changing this value may not have any effect upon reconfiguration. Identifies the location for the classloader to located the XML Schema to use to validate the configuration. Only valid when strict is set to true. If not set no schema validation will take place. Specifies whether or not Log4j should automatically shutdown when the JVM shuts down. The shutdown hook is enabled by default but may be disabled by setting this attribute to \The level of internal Log4j events that should be logged to the console. Valid values for this attribute are \initialization, rollover and other internal actions to the status logger. Setting status=\ is one of the first tools available to you if you need to troubleshoot log4j. dest monitorInterval name packages schema shutdownHook status strict verbose Enables the use of the strict XML format. Not supported in JSON configurations. Enables diagnostic information while loading plugins. Log4j2可以使用2种风格的XML配置,简洁风格和严格风格 。简洁风格的配置很简单,因为元素的名称就代表了他们匹配的组件,但是缺点是不能用xmlschema进行验证。 元素和属性名称是不区分大小写的 。另外,属性既可以被指定为一个XML的属性,也可以被指定为一个元素的文本值。 例如 : 简洁风格的配置 : 1. ; 2. 4. 13. 14. 15.