log4j2 使用详解

log4j升级了,新架构很不错,将安装步骤记录如下:

1、Log4j2的导入

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

2、测试用例

log4j 2.0的使用非常简单,只要用LogManager的getLogger函数获取一个logger,就可以使用logger记录日志,代码如下:

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

public class HelloLog4j {

private static Logger logger = LogManager.getLogger("HelloLog4j");

public static void main(String[] args) {

 MyApplication myApplication = new MyApplication();

 logger.entry();

 logger.info("Hello, World!");

 myApplication.doIt();

    logger.error("Hello, World!");

    logger.exit();

}

}

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

public class MyApplication {

  static Logger logger = LogManager.getLogger(MyApplication.class.getName()); 

  

  public boolean doIt() { 

   logger.entry();  //Log entry to a method 

   logger.error("Did it again!");  //Log a message object with the ERROR level 

   logger.exit();  //Log exit from a method 

   return false; 

  } 

}

运行程序,输出结果为:

12:11:28.672 [main] ERROR MyApplication - Did it again!

12:11:28.672 [main] ERROR HelloLog4j - Hello, World!

注意到,输出的log都是在ERROR level上的,log4j定义了8个级别的log(除去OFF和ALL,可以说分为6个级别),优先级从高到低依次为:OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE、 ALL。如果将log level设置在某一个级别上,那么比此级别优先级高的log都能打印出来。例如,如果设置优先级为WARN,那么OFF、FATAL、ERROR、WARN 4个级别的log能正常输出,而INFO、DEBUG、TRACE、 ALL级别的log则会被忽略。从我们实验的结果可以看出,log4j默认的优先级为ERROR或者WARN(实际上是ERROR)。

3. 配置文件

log4j是apache的一个开源项目,在写这篇博客的时候已经发布了2.0的beta版本,首先需要注意的是,log4j 2.0与以往的1.x有一个明显的不同,其配置文件只能采用.xml, .json或者 .jsn。在默认情况下,系统选择configuration文件的优先级如下:(classpath为scr文件夹)classpath下名为 log4j-test.json 或者log4j-test.jsn文件classpath下名为 log4j2-test.xmlclasspath下名为 log4j.json 或者log4j.jsn文件classpath下名为 log4j2.xml必须注意.xml 文件的文件名为log4j2,这里让我纠结了一个下午,系统一直找不到配置文件,最后发现是文件名里面少了一个2。下面以log4j2.xml为例来介绍log4j的配置。

<?xml version="1.0" encoding="UTF-8"?>

<configuration status="OFF">

 <appenders>

  <Console name="Console" target="SYSTEM_OUT">

   <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>

  </Console>

 </appenders>

 <loggers>

  <root level="trace">

   <appender-ref ref="Console"/>

  </root>

 </loggers>

</configuration>

首先介绍loggers标签,用于定义logger的lever和所采用的appender,其中appender-ref必须为先前定义的appenders的名称,例如,此处为Console。那么log就会以appender所定义的输出格式来输出log。root标签为log的默认输出形式,如果一个类的log没有在loggers中明确指定其输出lever与格式,那么就会采用root中定义的格式。例如以下定义:

<?xml version="1.0" encoding="UTF-8"?>

<configuration status="OFF">

 <appenders>

  <Console name="Console" target="SYSTEM_OUT">

   <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>

  </Console>

 </appenders>

 <loggers>

  <logger name="com.relin.HelloLog4j" level="error" additivity="false">

   <appender-ref ref="Console"/>

  </logger>

  <root level="trace">

   <appender-ref ref="Console"/>

  </root>

 </loggers>

</configuration>

此时,HelloLog4j则会在error级别上输出log,而其他类则会在trace级别上输出log。需要注意的是 additivity选项,如果设置为true(默认值)则HelloLog4j的log会被打印两次,第二次打印是由于HelloLog4j同时也满足root里面定义的trace

4. 其他特征

<?xml version="1.0" encoding="UTF-8"?> 

<configuration status="error"> 

 <appenders> 

<!--控制台输出-->

  <Console name="Console" target="SYSTEM_OUT"> 

   <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/> 

   <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> 

  </Console> 

<!--文件输出-->

  <File name="log" fileName="target/test.log" append="false"> 

   <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> 

  </File> 

<!--归档文件输出-->

  <RollingFile name="RollingFile" fileName="logs/app.log" 

         filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz"> 

   <PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/> 

   <SizeBasedTriggeringPolicy size="500 MB" /> 

  </RollingFile> 

 </appenders> 

 <loggers> 

  <root level="trace"> 

   <appender-ref ref="RollingFile"/> 

   <appender-ref ref="Console"/> 

  </root> 

 </loggers> 

</configuration>

注意:输出的日志文件默认存放在tomcat/bin目录下

扩展组件

1,ConsoleAppender

输出结果到System.out或是System.err。

2,FileAppender

输出结果到指定文件,同时可以指定输出数据的格式。append=“false”指定不追加到文件末尾

3,RollingFileAppender

自动追加日志信息到文件中,直至文件达到预定的大小,然后自动重新生成另外一个文件来记录之后的日志。

过滤标签

1,ThresholdFilter

用来过滤指定优先级的事件。

2,TimeFilter

设置start和end,来指定接收日志信息的时间区间。

最新评论:

no.116353 nxrxndt 202.51.120.22 2020-03-29 17:17

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116350 wjdpfcg 176.36.210.201 2020-03-29 14:42

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116349 cgfqduq 43.225.195.90 2020-03-29 13:50

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116343 ezmtdvk 177.66.79.201 2020-03-29 12:04

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116342 fqhzmew 110.172.160.42 2020-03-29 11:13

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116339 gzlanwm 200.77.186.206 2020-03-29 09:33

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116338 kckygiv 93.190.58.4 2020-03-29 08:47

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116337 nheonxf 176.98.156.64 2020-03-29 06:53

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116331 yqvyzaa 95.31.119.210 2020-03-29 02:08

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116328 mkim63399 134.249.233.205 2020-03-28 23:35

Жизнь с запущенным раком простаты может быть подавляющей. Важно понимать как болезнь, так и ваше лечение, чтобы вы могли принимать активное участие в принятии решений о вашем лечении. Если уровень простат-специфического антигена (ПСА) постоянно повышается во время лечения, которое снижает уровень тестостерона, это может означать, что ваш рак простаты прогрессирует. Прогрессирование означает, что рак ухудшается или распространяется. Когда это произойдет, ваше лечение может измениться. Вот

no.116327 dsftsci 62.168.65.194 2020-03-28 23:31

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116323 bfhzpib 185.37.211.222 2020-03-28 21:08

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116322 lbfrgqj 84.242.183.146 2020-03-28 20:09

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116317 ntgskxb 43.252.156.164 2020-03-28 18:34

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116306 kpjzbih 79.101.45.94 2020-03-28 13:13

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116302 macihnt 186.42.120.170 2020-03-28 10:56

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116301 ltugwja 96.9.75.231 2020-03-28 10:01

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116297 vxwnmpb 195.117.107.182 2020-03-28 07:24

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116292 knnmvxz 77.93.235.34 2020-03-28 05:47

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116291 pmydojy 103.77.159.59 2020-03-28 04:53

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116284 ippnwde 202.146.245.156 2020-03-28 00:57

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116279 mariaramble 37.115.220.72 2020-03-27 19:56

Приветствую вас Нашел самую не дорогую аптеку Лучшая Интернет Аптека [url=https://anticancer24.ru/shop/467/desc/cyclorine]циклосерин дозировка[/url]

no.116274 zyvzqtl 129.205.135.171 2020-03-27 17:03

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116271 hethertan 37.115.220.72 2020-03-27 14:34

Хорошие специалисты и прекрасный севрис в этой онко аптеке [url=https://india-express.net/shop/98/desc/brigatinib90]ленвима инструкция[/url]

no.116264 xtrrkrr 109.87.78.144 2020-03-27 11:58

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116262 robertagendick 5.248.224.61 2020-03-27 09:45

Оочень хороший препарат, побочек вообще небыло [url=https://india-express.net/shop/190/desc/dasatinib]тубацин ci 994 эрлотиниб +и дазатиниб[/url]

no.116258 hgwwaco 103.83.178.174 2020-03-27 09:23

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116248 uxdwarl 181.176.164.178 2020-03-27 03:32

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116244 dembagassamba4525 84.17.61.187 2020-03-27 00:05

Si vous êtes extrêmement fiable sur les trajets quotidiens en public, alors ce message sera certainement une révélation pour vous ainsi que pour divers autres lecteurs. Comme nous comprenons tous que les taxis sont aujourd'hui devenus largement présents dans divers autres modes de transport routier, et on peut compter sur eux sans attendre, de nos jours, il existe une série de raisons pour lesquelles de nombreuses personnes choisissent des services de taxi réputés ainsi que quelques-uns d'entr

no.116237 bbexmoe 41.33.212.78 2020-03-26 20:47

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116234 krjxyux 123.213.70.176 2020-03-26 18:18

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116221 qnetipx 93.41.193.212 2020-03-26 12:21

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116202 spnfaqv 177.86.115.2 2020-03-26 03:08

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116196 qvzbpys 103.123.169.165 2020-03-25 22:43

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116194 lajrihk 109.102.254.170 2020-03-25 21:49

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116185 qwmsgdw 190.104.237.148 2020-03-25 17:25

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116174 vyoirss 177.86.115.2 2020-03-25 12:18

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116172 cphprdj 210.245.51.20 2020-03-25 11:30

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116158 iuwxtuc 103.31.157.210 2020-03-25 06:35

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116155 mbspjuz 121.138.155.41 2020-03-25 04:56

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116150 wexjrxi 138.99.93.224 2020-03-25 02:19

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116136 maimjjb 103.70.204.194 2020-03-24 18:11

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116133 ocoyrlq 188.32.145.79 2020-03-24 16:14

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116127 aromyav 45.165.152.26 2020-03-24 12:37

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116120 ashvyua 91.216.66.70 2020-03-24 10:58

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116115 lkosycj 177.124.225.106 2020-03-24 08:40

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116111 nywiefs 177.73.248.26 2020-03-24 05:09

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116106 boujcug 36.66.216.82 2020-03-24 03:26

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116104 dodgfmz 94.141.120.28 2020-03-24 02:35

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

no.116102 mlizmyj 178.252.75.13 2020-03-24 00:58

[url=https://www.instagram.com/batmanapollo/]Психолог онлайн[/url] https://www.instagram.com/batmanapollo/ <a href="https://www.instagram.com/batmanapollo/">Психолог онлайн</a>

发表评论:

邮箱: