log4j2 配置文件

首先pom文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javacodegeeks.snippets.enterprise</groupId>
    <artifactId>log4jexample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.0.2</version>
  </dependency>
</dependencies>
</project>
 
   

 

然后是log4j2.xml文件

 

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
 <appenders>
  <Console name="Console" target="SYSTEM_OUT">
   <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
  </Console>

<Properties>
<Property name="log-path">D:/软件/workspaceForWeb/Test</Property>
</Properties>

  <RollingFile name="WroxFileAppender" fileName="${log-path}/myexample.log" filePattern="${log-path}/myexample-%d{yyyy-MM-dd}-%i.log" >
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1 KB" />
</Policies>
<DefaultRolloverStrategy max="4"/>
</RollingFile>


 </appenders>


 <Loggers>
  <Root level="trace">
   <AppenderRef ref="Console" />
  </Root>
   <logger name="com.wrox" levels="info" additivity="false">
     <appender-ref ref="RollingFile"/>
     <appender-ref ref="Console">
      <!-- 下变是个filter,说明了 可以logger to console appender,but only for event containing a Marker named EROX_CONSOLE-->
       <MarkerFilter marker="WORK_CONSOLE" onMathc="NEUTRAL" onMIsmath="DENY"/>
     </appender-ref>
   </logger> 
  <Logger name="yourLogger" level="debug" additivity="false">
   <AppenderRef ref="Console" />
  </Logger>
 </Loggers>


</configuration>

 

 

 

需要说明几点:

0、首先注意 configuration  的status--logging system本身的message,

记录日志系统本身的log,叫做StatusLogger,目的是log events occurring within eh logging system itself,比如说你建立了一个socket Appender然后目的网路不可到达这个日志就记录再次,利用StatusLogger,默认设置是OFF,也就是suppresses all the logging system message。

1、首先在appender中 配置roll the log---RollingFile 

roll log发生的时机是

the log reaching a certain size; 比如这里10m

the data changing;

the supplication starting;

3者中的任意组合

2、logger中的markerfiler:

下变是个filter,说明了 可以logger to console appender,but only for event containing a Marker named EROX_CONSOLE

3、注意日志格式:PatternLayout  

 %l prints the class,method,file and line number where the logging message occurred

4、关于logger继承关系

4.1、默认子logger是继承父亲的

if the root logger is assigned to  a console Appender and

com.wrox is assigned to file Appender,

log messages written to com.wrox,com.wrox.chat and others go to both the console and the file,

whereas log messages written to root or com.example.test go only to the console.

4.2、可以关掉这个继承利用additivity,表示该节点以及子节点不会在继承该节点的的父亲节点了

<logger name="com.wrox" levels="info" additivity="false">
     <appender-ref ref="WroxFileAppender"/>
     <appender-ref ref="Console">
     </appender-ref>
  </logger> 
注意属性 additivity,表示是否继承父亲属性com.wrox的属性additivity=false,那么com.wrox,com.wrox.chat,and others go only to the file,表示父继承关闭了

4.3、如果

root logger console appender &&

(com.wrox 的additivity=false &&分配给to file Appender) &&

( com.wrox.shop的属性 additivity=true && has a syslog appender),

那么消息 written to com.wrox.shop 会去syslog and the file,不会到console

5、关于java中使用

private static final Logger log=LogManager.getLogger();

 

log.error("");

 




转载于:https://www.cnblogs.com/amazement/p/4871965.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值