首页 > 代码库 > Log4j 2

Log4j 2

# 模板

 

<?xml version="1.0" encoding="UTF-8"?>
<!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,你会看到log4j2内部各种详细输出。-->
<!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数。-->
<configuration status="error" monitorInterval="30">
    <!--先定义所有的appender-->
    <appenders>
        <!--这个输出控制台的配置-->
        <Console name="Console" target="SYSTEM_OUT">
            <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
            <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            <!--这个都知道是输出日志的格式-->
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </Console>
        <!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用-->
        <File name="log" fileName="log/test.log" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </File>
        <!-- 这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
        <RollingFile name="RollingFile" fileName="logs/app.log"
                     filePattern="log/%d{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="50MB"/>
            <!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
            <DefaultRolloverStrategy max="20"/>
        </RollingFile>
    </appenders>
    <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
    <loggers>
        <!--建立一个默认的root的logger-->
        <root level="trace">
            <appender-ref ref="RollingFile"/>
            <appender-ref ref="Console"/>
        </root>
    </loggers>
</configuration>

 

# Spring 集成

 

    <listener>
        <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
    </listener>
    <filter>
        <filter-name>log4jServletFilter</filter-name>
        <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>log4jServletFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

 

# 模板代码

 

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;    
    }    
} 

 

 

# Reference

 

https://my.oschina.net/xianggao/blog/523401 Log4j2配置文件详解

http://blog.csdn.net/lrenjun/article/details/8178875 Log4j2使用详解

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

https://logging.apache.org/log4j/2.x/log4j-api/apidocs/index.html Log4j 2 API

https://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/Logger.html Interface Logger

https://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/Marker.html Interface Marker

Log4j 2