关于Spring5.0的Log4jConfigListener过期问题

2021-07-14 13:04

阅读:402

标签:lang   pen   port   static   filename   监听   base   1.0   policy   

(转自:https://blog.csdn.net/zhiyuzhe/article/details/78850238)

升级到spring5.0,发现之前web.xml 里配置的log监听Log4jConfigListener被spring废弃了,百度了一下,没有发现答案。 
这个是web.xml之前的配置:

    context-param> 
        param-name>log4jConfigLocationparam-name>
        param-value>classpath:log4j.propertiesparam-value>
    context-param>
    context-param>  
        param-name>webAppRootKeyparam-name>
        param-value>webApp.rootparam-value>
    context-param>
    context-param>
        param-name>log4jRefreshIntervalparam-name>
        param-value>6000param-value>
    context-param>
    listener>
        listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>
    listener> 

Spring5.0的api里已经没有这个方法了,切换到版本4.3.13,发现原文是这么说的。spring建议用log4j2 来替换这个类。以下为官方原文:

Deprecated. 
as of Spring 4.2.1, in favor of Apache Log4j 2 (following Apache‘s EOL declaration for log4j 1.x)

@Deprecated
public class Log4jConfigListener
extends java.lang.Object
implements ServletContextListener

于是看了一下log4j2,实现过程记录一下: 
首先引入全新的log4j2 依赖jar包

     dependency>
        groupId>org.apache.logging.log4jgroupId>
        artifactId>log4j-apiartifactId>
        version>2.10.0version>
    dependency>
    dependency>
        groupId>org.apache.logging.log4jgroupId>
        artifactId>log4j-coreartifactId>
        version>2.10.0version>
    dependency>

    dependency>
      groupId>org.apache.logging.log4jgroupId>
      artifactId>log4j-webartifactId>
      version>2.9.1version>
    dependency>

web.xml 的配置如下


     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>
     

src/main/resources`目录下新增log4j2.xml 文件,这个文件名是官方默认的

xml version="1.0" encoding="UTF-8"?>
Configuration status="OFF" monitorInterval="1800">
    properties>
        property name="LOG_HOME">/WEB-INF/logsproperty>
        property name="FILE_NAME">finance-payproperty>
    properties>


    Appenders>
        Console name="Console" target="SYSTEM_OUT">
            PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        Console>

        RollingFile name="running-log" fileName="${LOG_HOME}/${FILE_NAME}.log"
            filePattern="${LOG_HOME}/$${date:yyyy-MM}/${FILE_NAME}-%d{yyyy-MM-dd}-%i.log.gz"
            immediateFlush="true">
            PatternLayout
                pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
            Policies>
                TimeBasedTriggeringPolicy />
                SizeBasedTriggeringPolicy size="10 MB" />
            Policies>
            DefaultRolloverStrategy max="20" />
        RollingFile>
    Appenders>
    Loggers>

        

        Root level="info">
               
            AppenderRef ref="running-log" />
            
            AppenderRef ref="Console" />
        Root>
    Loggers>
Configuration>

编写一个测试类:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class TestLog4j2 {
     static Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

 public static void main(String[] args) {
         logger.trace("trace message");
         logger.debug("debug message");
         logger.info("info message");
         logger.warn("warn message");
         logger.error("error message");
         logger.fatal("fatal message");
         System.out.println("Hello World!");
     }

}

 

关于Spring5.0的Log4jConfigListener过期问题

标签:lang   pen   port   static   filename   监听   base   1.0   policy   

原文地址:https://www.cnblogs.com/chenloveslife/p/9538388.html


评论


亲,登录后才可以留言!