Spring5.x.x版本整合Log4J2

1.关于Log4J2的基本使用,请看我之前的文章:

https://blog.csdn.net/qq_41113081/article/details/89161244

2.Spring5版本之后废弃了很多方法,之前4.0版本的配置类已经废弃了,所以要重新配置:

(1)pom.xml导入依赖


		<dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-apiartifactId>
            <version>${log4j.version}version>
        dependency>

        <dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-coreartifactId>
            <version>${log4j.version}version>
        dependency>

        <dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-webartifactId>
            <version>${log4j.version}version>
        dependency>

(2)web.xml中配置


    <context-param>
        <param-name>log4jConfigurationparam-name>
        <param-value>classpath:log4j2.xmlparam-value>
    context-param>
    
    <listener>
        <listener-class>org.apache.logging.log4j.web.Log4jServletContextListenerlistener-class>
    listener>
    <filter>
        <filter-name>log4jServletFilterfilter-name>
        <filter-class>org.apache.logging.log4j.web.Log4jServletFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>log4jServletFilterfilter-name>
        <url-pattern>/*url-pattern>
        <dispatcher>REQUESTdispatcher>
        <dispatcher>FORWARDdispatcher>
        <dispatcher>INCLUDEdispatcher>
        <dispatcher>ERRORdispatcher>
    filter-mapping>

(3)在resources目录下新建log4j2.xml,名字要完全一致









<configuration status="TRACE" monitorInterval="30">
    
    <appenders>
        
        <console name="Console" target="SYSTEM_OUT">
            
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
        console>

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

        
        <RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/info.log"
                     filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
            
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>

            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            Policies>
        RollingFile>

        <RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/warn.log"
                     filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">

            <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            Policies>
            
            <DefaultRolloverStrategy max="20"/>
        RollingFile>

        <RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/error.log"
                     filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            Policies>
        RollingFile>

    appenders>

    
    <loggers>

        
        <logger name="org.springframework" level="INFO"/>
        <logger name="org.mybatis" level="INFO"/>

        <root level="all">
            
            <appender-ref ref="Console"/>
            
            <appender-ref ref="RollingFileInfo"/>
            
            <appender-ref ref="RollingFileWarn"/>
            
            <appender-ref ref="RollingFileError"/>
        root>

    loggers>

configuration>

(4)这样就配好了,具体使用方法:

static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
	    logger.trace("trace message");
        logger.debug("debug message");
        logger.info("info message");
        logger.warn("warn message");
        logger.error("error message");
        logger.fatal("fatal message");

你可能感兴趣的:(Spring)