ssm配置log4j日志报错java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener

错误日志:

严重: Error configuring application listener of class org.springframework.web.util.Log4jConfigListener
java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
	at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532)
	at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:514)
	at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:142)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4854)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

我用的版本     spring5+log4j

网上说spring5把web.xml 里配置的log监听Log4jConfigListener废弃了

spring建议用log4j2 来替换这个类

原来的配置:

web.xml

  
    
        log4jConfigLocation
        classpath:config/log4j.properties
    

    
    
        log4jRefreshInterval
        10000
    

    
    
        org.springframework.web.util.Log4jConfigListener
    

pom.xml

		
		
			org.slf4j
			slf4j-api
			1.7.21
		
		
			org.slf4j
			slf4j-log4j12
			1.7.21
		
		
		
			log4j
			log4j
			1.2.17
		

现在修改为log4j2   pom.xml


		
			org.slf4j
			slf4j-api
			1.7.21
		
		
			org.slf4j
			slf4j-log4j12
			1.7.21
		
		
		
			org.apache.logging.log4j
			log4j-api
			2.8.2
		
		
		
			org.apache.logging.log4j
			log4j-core
			2.8.2
		
		
		
			org.apache.logging.log4j
			log4j-web
			2.8.2
		

web.xml

	
	
		log4jConfiguration
		classpath:config/log4j2.xml
	
	
		org.apache.logging.log4j.web.Log4jServletContextListener
	
	
		log4jServletFilter
		org.apache.logging.log4j.web.Log4jServletFilter
	
	
		log4jServletFilter
		/*
		REQUEST
		FORWARD
		INCLUDE
		ERROR
	
	

设置拦截类

package com.fast.ssm.mybatis.log4j2;
 
import java.sql.Statement;
import java.util.Properties;
 
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
 
/**
 * Sql执行时间记录拦截器 
 */
@Intercepts({@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
 @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
 @Signature(type = StatementHandler.class, method = "batch", args = { Statement.class })})
public class SqlCostInterceptor implements Interceptor {
 
 @Override
 public Object intercept(Invocation invocation) throws Throwable {
   
  long startTime = System.currentTimeMillis();
  try {
   return invocation.proceed();
  } finally {
   long endTime = System.currentTimeMillis();
   long sqlCost = endTime - startTime;
   System.out.println("执行耗时 : [" + sqlCost + "ms ] ");
  }
 }
 
 @Override
 public Object plugin(Object target) {
  return Plugin.wrap(target, this);
 }
 
 @Override
 public void setProperties(Properties properties) {
   
 }
  
}

在spring和mybatis的配置文件中加上 拦截器 (我的文件名叫SqlMapConfig.xml) 

注意下面的这个标签的顺序,把它放到里面的最上面,否则启动的时候会报错。


	
        
    

		
		
	

如果出现这个报错

严重: Unable to process Jar entry [META-INF/versions/9/module-info.class] from Jar [jar:file:/D:/apache-maven-3.5.0/repository/org/apache/logging/log4j/log4j-api/2.11.0/log4j-api-2.11.0.jar!/] for annotations
org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
	at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:133)
	at org.apache.tomcat.util.bcel.classfile.ConstantPool.(ConstantPool.java:60)
	at org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:209)
	at org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:119)
	at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2134)
	at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2010)
	at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1976)
	at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1961)
	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1319)
	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:376)
	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

修改web.xml头文件  增加红色部分代码

metadata-complete="true">

修改pom.xml  log4j jar的版本   如果你选择的最高版本比如  2.11.0  就会出现这个问题

这里建议使用: 2.8.2 版本,一般就可以解决这个问题了。


spring5.0解决Log4jConfigListener过期问题博客:https://blog.csdn.net/zhiyuzhe/article/details/78850238

配置log4j2:https://www.rxblog.xyz/java-log4j2-web-xml/
                    https://blog.csdn.net/z69183787/article/details/52925567
                    https://blog.csdn.net/zjq852533445/article/details/78320012




你可能感兴趣的:(mybatis)