spring5.0解决Log4jConfigListener过期问题

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

    param> 
        <param-name>log4jConfigLocationparam-name>
        <param-value>classpath:log4j.propertiesparam-value>
    param>
    param>  
        <param-name>webAppRootKeyparam-name>
        <param-value>webApp.rootparam-value>
    param>
    param>
        <param-name>log4jRefreshIntervalparam-name>
        <param-value>6000param-value>
    param>
    
        org.springframework.web.util.Log4jConfigListener
     

上官网瞅了下,版本5的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 文件,这个文件名是官方默认的


<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!");
     }

}

发现已经写进去了,大功告成。

你可能感兴趣的:(java,web框架)