• 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("");

     




    我当记事本用的
  • 相关阅读:
    并发编程之守护进程、互斥锁以及队列等相关内容-37
    并发编程之进程理论及应用等相关内容-36
    补充知识之猴子补丁、内置函数以及垃圾回收机制等相关内容-35
    面向对象之元类等相关内容-34
    网络编程(套接字)之UDP协议通信以及基于socketserver模块实现并发效果等相关内容-33
    面向对象之组合、多态、以及内置函数及方法等相关内容-27
    面向对象之异常处理等相关内容-28
    网络基础之osi五层协议等相关内容-29
    网络编程(套接字)之TCP协议通信、远程执行命令等相关内容-31
    看到你很好,就行了,走啦!
  • 原文地址:https://www.cnblogs.com/amazement/p/4871965.html
Copyright © 2020-2023  润新知