Log4j2 — Log4j2导入、LogEvent、配置文件编写及路径

1. Log4j2的导入        

        首先到http://logging.apache.org/log4j/2.x/download.html 上下载最新的log4j2的jar包,然后再eclipse中加入log4j-api-2.4.1.jar和log4j-core-2.4.1.jar,需要注意的是不要将所有jar都导入工程造成不必要的混乱。

2.LogEvent

左边竖栏是Event的Level,右边横栏是LoggerConfig(即filter)的Level。Yes的意思就是这个event可以通过filter,no的意思就是不能通过filter。

可以看到,INFO级别的Event是无法被ERROR级别的LoggerConfig的filter接受的。所以,INFO信息不会被输出。

 

3.配置文件编写

 log4j2与以往的log4j有一个明显的不同,其配置文件只能采用.xml, .json或者 .jsn,而不是.properties文件。其格式如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <configuration status="error">
 3     <!--先定义所有的appender-->
 4     <appenders>
 5         <!--这个输出控制台的配置-->
 6         <Console name="Console" target="SYSTEM_OUT">
 7             <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
 8             <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
 9             <!--这个都知道是输出日志的格式-->
10             <PatternLayout pattern="%d{HH:mm:ss.SSS}  %-5level  %class{36}  %L %M ==>  %msg%xEx%n"/>
11         </Console>
12         <!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用-->
13         <File name="log" fileName="log/test.log" append="false">
14             <PatternLayout pattern="%d{HH:mm:ss.SSS}  %-5level  %class{36}  %L %M ==>  %msg%xEx%n"/>
15         </File>
16         <!--这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
17         <RollingFile name="RollingFile" fileName="logs/app.log"
18                      filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
19             <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
20             <SizeBasedTriggeringPolicy size="50MB"/>
21         </RollingFile>
22     </appenders>
23     <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
24     <loggers>
25         <!--建立一个默认的root的logger-->
26         <root level="trace">
27             <appender-ref ref="RollingFile"/>
28             <appender-ref ref="Console"/>
29         </root>
30     </loggers>
31 </configuration>
View Code

 

4.配置文件名称及路径

(1)放在classpath(src)下,以log4j2.xml命名

    使用Log4j2的一般都约定俗成的写一个log4j2.xml放在src目录下使用。

(2)将配置文件放到别处

    在系统工程里面,将log4j2的配置文件放到src目录底下很不方便。如果能把工程中用到的所有配置文件都放在一个文件夹里面,当然就更整齐更好管理了。但是想要实现这一点,前提就是Log4j2的配置文件能重新定位到别处去,而不是放在classpath底下。

看看文档里怎么说吧:

1.Log4j will inspect the "log4j.configurationFile" system property and, if set,will attempt to load the configuration using the ConfigurationFactory that matches the file extension.
2.If no system property is set the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
3.If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
4.If a test file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
5.If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
6.If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

可见,如果没有设置"log4j.configurationFile" system property的话,application将在classpath中按照如下查找顺序来找配置文件:

  log4j2-test.json 或log4j2-test.jsn文件

  log4j2-test.xml文件

  log4j2.json 或log4j2.jsn文件

  log4j2.xml文件

 

如果想将配置文件重命名并放到别处,就需要设置系统属性log4j.configurationFile。

设置的方式是在VM arguments中写入该属性的key和value:

-Dlog4j.configurationFile="D:learninglog20130115configLogConfig.xml"

在myeclipse中,就是 右键-》run as -》run configuration-》右边窗口的“(x)=argument”=》VM arguments

然后写入上述key和value即可。

-D是参数,不能缺少。

测试

在“D:learninglog20130115config”路径下编写文件:

root LoggerConfig的Level设为INFO。

在myeclipse中写入log4j.configurationFile系统属性:

测试的java程序如上文,在此不再重复。运行,console输出如下:

 

原文地址:https://www.cnblogs.com/hthuang/p/4932480.html