我正在尝试在Web应用程序上设置log4j。 Web服务器是Tomcat 7

我收到以下异常:

    log4j:ERROR Could not instantiate class [org.apache.log4j.varia.LevelMatchFilter    ].
    java.lang.ClassNotFoundException: org.apache.log4j.varia.LevelMatchFilter
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at org.apache.log4j.helpers.Loader.loadClass(Loader.java:198)
    at org.apache.log4j.helpers.OptionConverter.instantiateByClassName(OptionConverter.java:326)
    at org.apache.log4j.PropertyConfigurator.parseAppenderFilters(PropertyConfigurator.java:881)
    at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:812)
    at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:735)
    at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:615)
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:502)
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:547)
    at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:483)
    at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
    at org.apache.log4j.Logger.getLogger(Logger.java:104)
    at org.apache.commons.logging.impl.Log4JLogger.getLogger(Log4JLogger.java:262)
    at org.apache.commons.logging.impl.Log4JLogger.<init>(Log4JLogger.java:108)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.apache.commons.logging.impl.LogFactoryImpl.createLogFromClass(LogFactoryImpl.java:1025)
    at org.apache.commons.logging.impl.LogFactoryImpl.discoverLogImplementation(LogFactoryImpl.java:844)
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:541)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:292)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:269)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:657)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:269)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:649)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1585)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)


我将log4j.properties放在WEB-INF/classes目录中,就像许多SO用户在对其他问题的其他答案中建议的那样。

我将slf4j-api-1.7.5.jarslf4j-log4j12-1.7.5.jarlog4j-1.2.16.jar放在WEB-INF/lib目录中。

我的属性文件如下:

log4j.rootLogger = INFO,infoAppender,errAppender

log4j.appender.infoAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.infoAppender.File= D://logs//info//info.log
log4j.appender.infoAppender.DatePattern='_'dd-MM-yyyy
log4j.appender.infoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.infoAppender.layout.conversionPattern=%d{dd-MM-yyyy HH:mm:ss} %5p %m%n
log4j.appender.infoAppender.filter.infoFilter=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.infoAppender.filter.infoFilter.LevelToMatch=INFO
log4j.appender.infoAppender.filter.infoFilter.AcceptOnMatch=true
log4j.appender.infoAppender.filter.2=org.apache.log4j.varia.DenyAllFilter


log4j.appender.errAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.errAppender.File= D://logs//error/err.log
log4j.appender.errAppender.DatePattern='_'dd-MM-yyyy
log4j.appender.errAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.errAppender.layout.conversionPattern=%d{dd-MM-yyyy HH:mm:ss} %5p %m%n
log4j.appender.errAppender.filter.errFilter=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.errAppender.filter.errFilter.LevelToMatch=ERROR
log4j.appender.errAppender.filter.errFilter.AcceptOnMatch=true
log4j.appender.errAppender.filter.2=org.apache.log4j.varia.DenyAllFilter


我个人认为问题出在类路径问题中,但是所有.jar文件和.properties文件都位于正确的位置。至少,根据我发现的许多相关帖子,它是正确的。

最佳答案

我也遇到了这个问题,最后我发现根位于log4j.properties文件中。您的属性文件中有多余的空间。就像您的异常指出的那样:log4j:ERROR无法实例化类[org.apache.log4j.varia.LevelMatchFilter]
LevelMatchFilter之后没有多余的空间。

这是我的


log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.filter.a=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.stdout.filter.a.LevelToMatch=INFO
log4j.appender.stdout.filter.b=org.apache.log4j.varia.DenyAllFilter

关于java - Tomcat无法实例化org.apache.log4j.varia.LevelMatchFilter类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19946016/

10-14 07:43