Spring5解决Log4jConfigListener的问题

在把一个老项目的jar升级完后发现启动失败,Tomcat localhost log 信息如下:配置应用的监听失败

30-Jul-2020 17:37:18.362 严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener of class [org.springframework.web.util.Log4jConfigListener]
	java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener

日志这块之前都好好的,现在却报错,查看源码确实找不到这个类,这是原web.xml配置


	<context-param>
		<param-name>log4jConfigLocationparam-name>
		<param-value>classpath:log4j.propertiesparam-value>
	context-param>
	
	<context-param>
		<param-name>log4jRefreshIntervalparam-name>
		<param-value>60000param-value>
	context-param>
	
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>
	listener>

发现从Spring 4.2.1开始,已删除Log4jConfigListener,支持Apache Log4j 2,官方说明

所以我们需要引入log4j的jar,注意是apache.logging 下的,版本自定义,在pom.xml加入依赖


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

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

    
    <dependency>
      <groupId>org.apache.logging.log4jgroupId>
      <artifactId>log4j-webartifactId>
      <version>${log4j.version}version>
    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>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
    listener>
    

注意需要配置在spirng ContextLoaderListener之前 ,This listener should be registered before ContextLoaderListener in web.xml when using custom log4j initialization.

你可能感兴趣的:(java)