log4j2.xml 疑难解惑

①:当log4j程序在项目中未找到相应的配置时,就会启用默认配置

默认配置会把 console类型的appender关联到root logger上面去,默认启用ERROR的level,使用  (%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n) 格式进行日志输出

https://logging.apache.org/log4j/2.x/manual/configuration.html
Log4j will provide a default configuration if it cannot locate a configuration file.
The default configuration, provided in the DefaultConfiguration class, will set up:

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 ConsoleAppender
Note that by default Log4j assigns the root logger to Level.ERROR.


②:loggers里边的root和logger标签相当于父子标签,root负责所有的级别控制,logger通过包名进行级别控制.

 <Loggers>
     <Logger name="com.foo.Bar" level="trace">
       <AppenderRef ref="Console"/>
     </Logger>
     <Root level="error">
       <AppenderRef ref="Console"/>
     </Root>
  </Loggers>

可以通过logger修改spring,hibernate等框架自身日志打印级别

<logger name="org.springframework" level="off"></logger>
<logger name="org.hibernate" level="off"></logger>

当logger和root都应用了相同的appender的时候,日志就会打印两次,这时可以在logger上添加属性additivity="false"来禁止这种情况发生,当然最好是logger中不要添加单独的appender.

<Logger name="com.foo.Bar" level="trace" additivity="false">
      <AppenderRef ref="Console"/>
</Logger>

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

Notice that the trace messages from com.foo.Bar appear twice. This is because the appender associated with logger com.foo.Bar is first used, which writes the first instance to the Console. Next, the parent of com.foo.Bar, which in this case is the root logger, is referenced. The event is then passed to its appender, which is also writes to the Console, resulting in the second instance. This is known as additivity. While additivity can be quite a convenient feature (as in the first previous example where no appender reference needed to be configured), in many cases this behavior is considered undesirable and so it is possible to disable it by setting the additivity attribute on the logger to false:


③:log4j2.xml中如何配置不同的日志输出到不同日志文件

    这个分为两种,级别控制和包名控制.

    一,级别控制

          通过过ThresholdFilter进行组合过滤, level代表匹配大于等于该级别的日志,onMatch匹配到做什么, onMismatch没有匹配到做什么,三个值,   ACCEPT 接受, DENY 拒绝, NEUTRAL 中立,即向下执行,在有多个过滤器的时候必须完全匹配才行.下面的配置必须是等于info级别的时候才会写入到文件中.

    二,包名控制

        配置单独的logger,指定日志级别,additivity="false"代表这个包下面的日志不会再root logger中再次输出

         


④:log4j2.xml按(天/小时)记录日志

属性解释
interval(integer)该属性是相对 RollingFile.filePattern 中的 %d{yyyy-MM-dd}值,例:
filePattern=”xxx%d{yyyy-MM-dd}xx” interval=”2” 表示将2天一个日志文件;
filePattern=”xxx%d{yyyy-MM-dd-HH}xx” interval=”1”表示一个小时一个日志文件
modulate(boolean)以0点为边界进行偏移计算(是否以0点为参考)


⑤:log4j2指定配置文件位置

官方文档:http://logging.apache.org/log4j/2.x/faq.html#config_location

               http://logging.apache.org/log4j/2.x/manual/webapp.html#ContextParams

  1. :在项目的resource目录下(classpath)添加log4j2.component.properties文件
  2. 在web.xml中配置                                                                                                                                                                      

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值