当前位置: 首页>数据库>正文

log4j2读取properties log4j2 properties配置

官方文档:http://logging.apache.org/log4j/2.x/index.html

1 概述

Log4j2的配置包含四种方式,其中3种都是在程序中直接调用Log4j2的方法进行配置的,此处不作讲解,文档可见Extending Log4j 2 与 Programmatic Log4j Configuration;还有一种是我们最常用的用文件进行配置,可以是XML、JSON、YAML、properties format,

这里我们用XML为例来详细介绍。

2 默认配置

随着log4j2的start,log4j2会自动定位classpath下的log4j2.xml,若配置文件不存在,则取默认配置。

默认配置:

 

  • A ConsoleAppender attached to the root logger.
  • A PatternLayout set to the pattern "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" attached to the ConsoleAppend

若将默认配置改写为log4j2.xml,如下

<?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">
      <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="error">
          <AppenderRef ref="Console"/>
        </Root>
      </Loggers>
    </Configuration>

3 日志级别

Log4j2把日志打印分为6个级别,级别由低到高分别为trace < debug <info < warn < error < fatal,介绍如下:

  • trace:是追踪,就是程序推进以下,你就可以写个trace输出,所以trace应该会特别多,不过没关系,我们可以设置最低日志级别不让他输出。
  • debug:调试,一般用作最低级别,trace压根不用。
  • info:输出一下你感兴趣的或者重要的信息,这个用的最多了。
  • warn:有些信息不是错误信息,但是也要给程序员的一些提示,类似于eclipse中代码的验证不是有error和warn(不算错误但是也请注意,比如以下depressed的方法)。
  • error:错误信息。用的也比较多。
  • fatal:级别比较高了。重大错误,这种级别你可以直接停止程序了,是不应该出现的错误么!不用那么紧张,其实就是一个程度的问题。

规则说明:若root的级别为error,即 <Root level="error">

4 可加性(Additivity)

4.1 Additivity(待完善)

简单描述,当name1的logger输出时,此时若包含name2的输出,且name2的additivity为false,则name2的信息无法输出。

4.2 隔离输出

当我们需要某个包、某个类、某个名字下指定输出级别时,仅需在<Loggers></Loggers>节点中定义对应的日志记录器logger,logger用于记录范围。

<Loggers>
    <Logger name="包名|包名+类名|名称" level="trace">
        <AppenderRef ref="Console"/>
  </Logger>
    <Root level="error">
        <AppenderRef ref="Console"/>
    </Root>
</Loggers>

5 Appender

用于定义输出方式,如下:

  • 控制台
  • 文件(固定周期,每天使用一个日志文件记录;固定大小,超过指定大小则记录到一个新日志文件中)
  • 数据库
  • 远程日志接口程序

6.Configuration 属性

6.1 monitorInterval,自动重新加载配置(Automatic Reconfiguration)

Log4j2支持动态调整配置,自动识别加载,减少了线上系统重启的代价。设置monitorInterval,单位秒,最小5秒,如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30">
...
</Configuration>

6.2 status

设置为debug,用户可以在eclipse的控制台中看到log4j2启动与加载配置文件时的打印信息,如

log4j2读取properties log4j2 properties配置,log4j2读取properties log4j2 properties配置_xml,第1张

log4j2读取properties log4j2 properties配置,log4j2读取properties log4j2 properties配置_apache_02,第2张

2016-10-31 16:53:28,540 DEBUG Starting configuration XmlConfiguration[location=D:\work\source\log4j2-study\web\WEB-INF\classes\log4j2.xml]
2016-10-31 16:53:28,541 DEBUG PluginManager 'Core' found 69 plugins
2016-10-31 16:53:28,541 DEBUG PluginManager 'Level' found 0 plugins
2016-10-31 16:53:28,544 DEBUG PluginManager 'Lookup' found 10 plugins
2016-10-31 16:53:28,546 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout]. Searching for builder factory method...
2016-10-31 16:53:28,550 DEBUG Found builder factory method [newBuilder]: public static org.apache.logging.log4j.core.layout.PatternLayout$Builder org.apache.logging.log4j.core.layout.PatternLayout.newBuilder().
2016-10-31 16:53:28,565 DEBUG TypeConverterRegistry initializing.
2016-10-31 16:53:28,565 DEBUG PluginManager 'TypeConverter' found 21 plugins
2016-10-31 16:53:28,582 DEBUG Calling build() on class class org.apache.logging.log4j.core.layout.PatternLayout$Builder for element PatternLayout with params(name="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n", Configuration(D:\work\source\log4j2-study\web\WEB-INF\classes\log4j2.xml), null, name="null", name="null", name="null", name="null", name="null")
2016-10-31 16:53:28,582 DEBUG PluginManager 'Converter' found 33 plugins
2016-10-31 16:53:28,583 DEBUG Built Plugin[name=layout] OK from builder factory method.
2016-10-31 16:53:28,584 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender]. Searching for builder factory method...
2016-10-31 16:53:28,586 DEBUG Not in a ServletContext environment, thus not loading WebLookup plugin.
2016-10-31 16:53:28,586 DEBUG PluginManager 'Converter' found 33 plugins
2016-10-31 16:53:28,588 DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2016-10-31 16:53:28,588 DEBUG Found builder factory method [newBuilder]: public static org.apache.logging.log4j.core.appender.ConsoleAppender$Builder org.apache.logging.log4j.core.appender.ConsoleAppender.newBuilder().
2016-10-31 16:53:28,594 DEBUG No PluginVisitorStrategy found on annotation [interface org.apache.logging.log4j.core.config.plugins.validation.constraints.Required]. Ignoring.
2016-10-31 16:53:28,595 DEBUG Encountered type [org.apache.logging.log4j.core.layout.PatternLayout] which can only be checked for null.
2016-10-31 16:53:28,597 DEBUG No PluginVisitorStrategy found on annotation [interface org.apache.logging.log4j.core.config.plugins.validation.constraints.Required]. Ignoring.
2016-10-31 16:53:28,598 DEBUG Encountered type [org.apache.logging.log4j.core.appender.ConsoleAppender$Target] which can only be checked for null.
2016-10-31 16:53:28,598 DEBUG No PluginVisitorStrategy found on annotation [interface org.apache.logging.log4j.core.config.plugins.validation.constraints.Required]. Ignoring.
2016-10-31 16:53:28,599 DEBUG Calling build() on class class org.apache.logging.log4j.core.appender.ConsoleAppender$Builder for element Console with params(PatternLayout(%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n), null, name="SYSTEM_OUT", name="Console", name="null", name="null")
2016-10-31 16:53:28,600 DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2016-10-31 16:53:28,601 DEBUG Built Plugin[name=appender] OK from builder factory method.
2016-10-31 16:53:28,601 DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin]. Searching for builder factory method...
2016-10-31 16:53:28,604 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.AppendersPlugin. Going to try finding a factory method instead.
2016-10-31 16:53:28,604 DEBUG Still building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin]. Searching for factory method...
2016-10-31 16:53:28,605 DEBUG Found factory method [createAppenders]: public static java.util.concurrent.ConcurrentMap org.apache.logging.log4j.core.config.AppendersPlugin.createAppenders(org.apache.logging.log4j.core.Appender[]).
2016-10-31 16:53:28,605 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.AppendersPlugin for element Appenders with params(={Console})
2016-10-31 16:53:28,606 DEBUG Built Plugin[name=appenders] OK from factory method.
2016-10-31 16:53:28,606 DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig]. Searching for builder factory method...
2016-10-31 16:53:28,607 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggerConfig. Going to try finding a factory method instead.
2016-10-31 16:53:28,607 DEBUG Still building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig]. Searching for factory method...
2016-10-31 16:53:28,608 DEBUG Found factory method [createLogger]: public static org.apache.logging.log4j.core.config.LoggerConfig org.apache.logging.log4j.core.config.LoggerConfig.createLogger(java.lang.String,org.apache.logging.log4j.Level,java.lang.String,java.lang.String,org.apache.logging.log4j.core.config.AppenderRef[],org.apache.logging.log4j.core.config.Property[],org.apache.logging.log4j.core.config.Configuration,org.apache.logging.log4j.core.Filter).
2016-10-31 16:53:28,617 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element Logger with params(name="null", name="DEBUG", name="ted.log4j2.study.demo", name="null", ={}, ={}, Configuration(D:\work\source\log4j2-study\web\WEB-INF\classes\log4j2.xml), null)
2016-10-31 16:53:28,619 DEBUG Built Plugin[name=logger] OK from factory method.
2016-10-31 16:53:28,619 DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig]. Searching for builder factory method...
2016-10-31 16:53:28,620 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggerConfig. Going to try finding a factory method instead.
2016-10-31 16:53:28,620 DEBUG Still building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig]. Searching for factory method...
2016-10-31 16:53:28,621 DEBUG Found factory method [createLogger]: public static org.apache.logging.log4j.core.config.LoggerConfig org.apache.logging.log4j.core.config.LoggerConfig.createLogger(java.lang.String,org.apache.logging.log4j.Level,java.lang.String,java.lang.String,org.apache.logging.log4j.core.config.AppenderRef[],org.apache.logging.log4j.core.config.Property[],org.apache.logging.log4j.core.config.Configuration,org.apache.logging.log4j.core.Filter).
2016-10-31 16:53:28,622 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element Logger with params(name="null", name="INFO", name="test", name="null", ={}, ={}, Configuration(D:\work\source\log4j2-study\web\WEB-INF\classes\log4j2.xml), null)
2016-10-31 16:53:28,622 DEBUG Built Plugin[name=logger] OK from factory method.
2016-10-31 16:53:28,622 DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. Searching for builder factory method...
2016-10-31 16:53:28,623 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.AppenderRef. Going to try finding a factory method instead.
2016-10-31 16:53:28,623 DEBUG Still building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. Searching for factory method...
2016-10-31 16:53:28,624 DEBUG Found factory method [createAppenderRef]: public static org.apache.logging.log4j.core.config.AppenderRef org.apache.logging.log4j.core.config.AppenderRef.createAppenderRef(java.lang.String,org.apache.logging.log4j.Level,org.apache.logging.log4j.core.Filter).
2016-10-31 16:53:28,624 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(name="Console", name="null", null)
2016-10-31 16:53:28,625 DEBUG Built Plugin[name=AppenderRef] OK from factory method.
2016-10-31 16:53:28,625 DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger]. Searching for builder factory method...
2016-10-31 16:53:28,626 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger. Going to try finding a factory method instead.
2016-10-31 16:53:28,626 DEBUG Still building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger]. Searching for factory method...
2016-10-31 16:53:28,626 DEBUG Found factory method [createLogger]: public static org.apache.logging.log4j.core.config.LoggerConfig org.apache.logging.log4j.core.config.LoggerConfig$RootLogger.createLogger(java.lang.String,org.apache.logging.log4j.Level,java.lang.String,org.apache.logging.log4j.core.config.AppenderRef[],org.apache.logging.log4j.core.config.Property[],org.apache.logging.log4j.core.config.Configuration,org.apache.logging.log4j.core.Filter).
2016-10-31 16:53:28,628 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element Root with params(name="null", name="ERROR", name="null", ={Console}, ={}, Configuration(D:\work\source\log4j2-study\web\WEB-INF\classes\log4j2.xml), null)
2016-10-31 16:53:28,629 DEBUG Built Plugin[name=root] OK from factory method.
2016-10-31 16:53:28,629 DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin]. Searching for builder factory method...
2016-10-31 16:53:28,630 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggersPlugin. Going to try finding a factory method instead.
2016-10-31 16:53:28,630 DEBUG Still building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin]. Searching for factory method...
2016-10-31 16:53:28,631 DEBUG Found factory method [createLoggers]: public static org.apache.logging.log4j.core.config.Loggers org.apache.logging.log4j.core.config.LoggersPlugin.createLoggers(org.apache.logging.log4j.core.config.LoggerConfig[]).
2016-10-31 16:53:28,631 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.LoggersPlugin for element Loggers with params(={ted.log4j2.study.demo, test, root})
2016-10-31 16:53:28,632 DEBUG Built Plugin[name=loggers] OK from factory method.
2016-10-31 16:53:28,633 DEBUG Started configuration XmlConfiguration[location=D:\work\source\log4j2-study\web\WEB-INF\classes\log4j2.xml] OK.
2016-10-31 16:53:28,634 DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@72314fd4 OK
2016-10-31 16:53:28,705 DEBUG Registering MBean org.apache.logging.log4j2:type=150838093
2016-10-31 16:53:28,708 DEBUG Registering MBean org.apache.logging.log4j2:type=150838093,component=StatusLogger
2016-10-31 16:53:28,710 DEBUG Registering MBean org.apache.logging.log4j2:type=150838093,component=ContextSelector
2016-10-31 16:53:28,712 DEBUG Registering MBean org.apache.logging.log4j2:type=150838093,component=Loggers,name=test
2016-10-31 16:53:28,713 DEBUG Registering MBean org.apache.logging.log4j2:type=150838093,component=Loggers,name=
2016-10-31 16:53:28,714 DEBUG Registering MBean org.apache.logging.log4j2:type=150838093,component=Loggers,name=ted.log4j2.study.demo
2016-10-31 16:53:28,715 DEBUG Registering MBean org.apache.logging.log4j2:type=150838093,component=Appenders,name=Console
2016-10-31 16:53:28,715 DEBUG Reconfiguration complete for context[name=150838093] at null (org.apache.logging.log4j.core.LoggerContext@60b964af) with optional ClassLoader: null
2016-10-31 16:53:28,716 DEBUG Shutdown hook enabled. Registering a new one.
2016-10-31 16:53:28,718 DEBUG LoggerContext[name=150838093, org.apache.logging.log4j.core.LoggerContext@60b964af] started OK.
2016-10-31 16:53:28,726 DEBUG Using default SystemClock for timestamps
16:53:28.734 [main] ERROR ted.log4j2.study.demo.DemoTest - error
16:53:28.735 [main] DEBUG ted.log4j2.study.demo.DemoTest - debug
16:53:28.735 [main] WARN  ted.log4j2.study.demo.DemoTest - warn
16:53:28.735 [main] INFO  ted.log4j2.study.demo.DemoTest - info
2016-10-31 16:53:28,739 DEBUG Stopping LoggerContext[name=150838093, org.apache.logging.log4j.core.LoggerContext@60b964af]
2016-10-31 16:53:28,739 DEBUG Stopping LoggerContext[name=150838093, org.apache.logging.log4j.core.LoggerContext@60b964af]...
2016-10-31 16:53:28,740 DEBUG Unregistering MBean org.apache.logging.log4j2:type=150838093
2016-10-31 16:53:28,740 DEBUG Unregistering MBean org.apache.logging.log4j2:type=150838093,component=StatusLogger
2016-10-31 16:53:28,740 DEBUG Unregistering MBean org.apache.logging.log4j2:type=150838093,component=ContextSelector
2016-10-31 16:53:28,741 DEBUG Unregistering MBean org.apache.logging.log4j2:type=150838093,component=Loggers,name=ted.log4j2.study.demo
2016-10-31 16:53:28,741 DEBUG Unregistering MBean org.apache.logging.log4j2:type=150838093,component=Loggers,name=test
2016-10-31 16:53:28,741 DEBUG Unregistering MBean org.apache.logging.log4j2:type=150838093,component=Loggers,name=
2016-10-31 16:53:28,743 DEBUG Unregistering MBean org.apache.logging.log4j2:type=150838093,component=Appenders,name=Console
2016-10-31 16:53:28,745 DEBUG Stopped XmlConfiguration[location=D:\work\source\log4j2-study\web\WEB-INF\classes\log4j2.xml] OK
2016-10-31 16:53:28,745 DEBUG Stopped LoggerContext[name=150838093, org.apache.logging.log4j.core.LoggerContext@60b964af]...

View Code

7.配置日志输出格式(Pattern)

  • %d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyyy-MM-dd HH:mm:ss},输出类似:2006-01-18 17:50:22'
  • %t 产生该日志事件的线程名
  • %p 日志的log_level,如DEBUG、WARN或者INFO
  • %c 输出所属的类目,通常就是所在类的全名,如“com.mlq.love”
  • %m 日志的内容,同%msg
  • %l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。如Wite2Database.main(Wite2Database.java:18)
  • %n 输出一个回车换行符
  • %M 方法名称
  • %xEx The same as the %throwable conversion word but also includes class packaging information.

8 属性替换(PropertySubstitution)

$${date:yyyy-MM}




https://www.xamrdz.com/database/6q61923928.html

相关文章: