log4j2使用手册@zhangsf(9)

2020-04-14 16:45

PatternLayout提供了打印 Thread Context Map 和Thread Context Stack 内容的方法:

? ? ? Use %X by itself to include the full contents of the Map. Use %X{key} to include the specified key. Use %x to include the full contents of the Stack.

四. Configuration

本章只介绍常用和特殊的配置, 全部内容参考 :

http://logging.apache.org/log4j/2.x/manual/configuration.html

插入到应用中的日志代码,需要很合理的规划。据统计,应用中有大约 百分之四 的代码是专门用来记录日志的 。因此即便是一个较小规模的应用,其中也有很多的日志代码。因此, 不用手工修改的去管理这些日志代码成为一种必要。

Log4j2可以通过4种方式进行配置 :

(1) 通过 XML, JSON, YAML,或者 Properties的配置文件 。

(2)编程方式, 通过创建 一个ConfigurationFactory和 Configuration的实现。

(3)编程方式,通过调用 Configuration暴露的API, 为默认的 configuration增加组件。 (4)编程方式,通过调用 Logger的内部方法 。

本章主要介绍通过配置文件的方式配置Log4j2

1. 自动配置

Log4j2可以在初始化的时候自动完成配置,当log4j2启动时,会定位所有的 ConfigurationFactory的配置,并且按照优先级的高底加载它们。Log4j2包括四种 ConfigurationFactory的实现,JSON, YAML, properties,XML。

1. Log4j2将从system property(JVM启动参数,或者System.setProperty设置)读取

log4j.configurationFile

属性,如果设置了对应的文件,Log4j2

ConfigurationFactory 将尝试根据文件扩展名来加载文件。

2. 如果systemproperty 没有设置,propertiesConfigurationFactory 将在classpath中查

找 log4j2-test.properties文件。 3. 如果没有找到,YAMLConfigurationFactory 将在 classpath

log4j2-test.yaml或者log4j2-test.yml文件。

中查找

4. 如果没有找到, JsonConfigurationFactory 将在

log4j2-test.json 或者log4j2-test.jsn

classpath中查找

5. 如果没有找到XML ConfigurationFactory 将在 classpath中查找log4j2-test.xml 6. 如果 test文件找不到,则PropertiesConfigurationFactory 将在classpath中查找

log4j2.properties文件

7. 如果未找到,YAML ConfigurationFactory 将在classpath中查找log4j2.yaml或者

log4j2.yml

8. 如果未找到,JSON ConfigurationFactory 将在 classpath中查找log4j2.json或者

log4j2.jsn

9. 如果未找到,XML ConfigurationFactory 将在classpath中查找log4j2.xml 10. 如果都未找到,DefaultConfiguration 将被使用,默认将日志输出到 console控制台。

例子 : MyApp 使用log4j2 1. importcom.foo.Bar; 2.

3. // Import log4j classes.

4. importorg.apache.logging.log4j.Logger; 5. importorg.apache.logging.log4j.LogManager; 6.

7. publicclassMyApp{ 8.

9. // Define a static logger variable so that it references the 10. // Logger instance named \

11. privatestaticfinalLogger logger =LogManager.getLogger(MyApp.class); 12.

13. publicstaticvoidmain(finalString... args){ 14.

15. // Set up a simple configuration that logs on the console. 16.

17. logger.trace(\); 18. Bar bar =newBar(); 19. if(!bar.doIt()){

20. logger.error(\); 21. }

22. logger.trace(\); 23. } 24. }

MyApp 引入log4j2相关的类,在类中定义了一个名为MyApp 的静态 Logger, 另外一个类 Bar 内容如下 : 1. package com.foo;

2. importorg.apache.logging.log4j.Logger; 3. importorg.apache.logging.log4j.LogManager; 4.

5. publicclassBar{

6. staticfinalLogger logger =LogManager.getLogger(Bar.class.getName()); 7.

8. publicbooleandoIt(){ 9. logger.entry();

10. logger.error(\); 11. returnlogger.exit(false); 12. } 13. }

如果没有加载任何配置文件,log4j将提供一个默认的configuration 。默认的configuration 是通过DefaultConfiguration 定义的,将设置如下 : ? Root Logger将使用 ConsoleAppender

? ConsoleAppender 将使用PatternLayout的格式是 : \? 日志级别是 Level.ERROR

所以上面例子打印如下 :

17:13:01.540 [main] ERROR com.foo.Bar - Did it again! 17:13:01.540 [main] ERROR MyApp - Didn't do it.

默认配置相当于如下的 XML配置 :

1. 2. 3.

4.

5.

- %msg%n\/> 6. 7. 8.

9.

10. 11. 12. 13.

2. Additivity

假设只想将 com.foo.Bar的日志级别设置为 TRACE, 仅仅改变日志级别就不能达到这样的要求 ,解决方案是新增一个 Logger。

1. 2.

3. 4.

使用这个配置,com.foo.Bar 的所有日志将被记录,而其他模块只有 Error日志会被记录。 在这个配置中 ,com.foo.Bar 仍然将日志打印到 Console中,因为 com.foo.Bar并没有配置任何appenders ,所以就会使用 Root Logger的配置。事实上,如下配置

1. 2. 3.

4.

5.

- %msg%n\/> 6. 7. 8.

9. 10. 11.

12.

13. 14.


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

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

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

马上注册会员

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