简体   繁体   English

无法初始化log4j

[英]Unable to initialize log4j

I'm trying to initialize log4j-1.2.8 for my application by creating a listener class implemented from the ApplicationLifecycleListener of Weblogic 9.2. 我正在尝试通过创建一个从Weblogic 9.2的ApplicationLifecycleListener实现的侦听器类来为我的应用程序初始化log4j-1.2.8。 When I deploy the application, I'm getting following exceptions: 部署应用程序时,我遇到以下异常:

java.lang.NoClassDefFoundError: org/apache/log4j/spi/RepositorySelector
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at weblogic.utils.classloaders.GenericClassLoader.defineClass(GenericClassLoader.java:338)
at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:291)
at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:259)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at weblogic.utils.classloaders.GenericClassLoader.loadClass(GenericClassLoader.java:158)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at myApp.LoggerStartupService.preStart(LoggerStartupService.java:40)
at weblogic.application.internal.flow.BaseLifecycleFlow$PreStartAction.run(BaseLifecycleFlow.java:187)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.application.internal.flow.BaseLifecycleFlow$BaseAction.invoke(BaseLifecycleFlow.java:95)
at weblogic.application.internal.flow.BaseLifecycleFlow.preStart(BaseLifecycleFlow.java:53)
at weblogic.application.internal.flow.HeadLifecycleFlow.prepare(HeadLifecycleFlow.java:199)
at weblogic.application.internal.BaseDeployment$1.next(BaseDeployment.java:615)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:26)
at weblogic.application.internal.BaseDeployment.prepare(BaseDeployment.java:191)
at weblogic.application.internal.DeploymentStateChecker.prepare(DeploymentStateChecker.java:147)
at weblogic.deploy.internal.targetserver.AppContainerInvoker.prepare(AppContainerInvoker.java:61)
at weblogic.deploy.internal.targetserver.operations.ActivateOperation.createAndPrepareContainer(ActivateOperation.java:189)
at weblogic.deploy.internal.targetserver.operations.ActivateOperation.doPrepare(ActivateOperation.java:87)
at weblogic.deploy.internal.targetserver.operations.AbstractOperation.prepare(AbstractOperation.java:217)
at weblogic.deploy.internal.targetserver.DeploymentManager.handleDeploymentPrepare(DeploymentManager.java:718)
at weblogic.deploy.internal.targetserver.DeploymentManager.prepareDeploymentList(DeploymentManager.java:1185)
at weblogic.deploy.internal.targetserver.DeploymentManager.handlePrepare(DeploymentManager.java:247)
at weblogic.deploy.internal.targetserver.DeploymentServiceDispatcher.prepare(DeploymentServiceDispatcher.java:157)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.doPrepareCallback(DeploymentReceiverCallbackDeliverer.java:157)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.access$000(DeploymentReceiverCallbackDeliverer.java:12)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer$1.run(DeploymentReceiverCallbackDeliverer.java:45)
at weblogic.work.ServerWorkManagerImpl$WorkAdapterImpl.run(ServerWorkManagerImpl.java:518)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)

where myApp.LoggerStartupService is the class implemented from ApplicationLifecycleListener . 其中myApp.LoggerStartupService是从ApplicationLifecycleListener实现的类。

I checked the classpath in my scripts and its set properly for all dependencies required for log4j. 我检查了我的脚本中的类路径及其设置,以适应log4j所需的所有依赖项。 Appenders & Categories are also there in the log4j.xml. log4j.xml中也有Appenders&Categories。 Looks like I'm missing something. 看起来我错过了什么。 Any ideas on what the problem may be? 关于问题可能是什么的任何想法?

 java.lang.NoClassDefFoundError: org/apache/log4j/spi/RepositorySelector 

This means that the in the message mentioned class is missing in the classpath during runtime (while it was available during compiletime of the calling class in question, that's the difference with ClassNotFoundException ). 这意味着在运行期间类路径中缺少所提到的消息中的类(虽然它在所讨论的调用类的编译期间可用,但这与ClassNotFoundException不同)。

As this is used by Log4j itself , it's thus missing in the Log4j JAR file. 由于Log4j 本身使用 ,因此它在Log4j JAR文件中丢失。 The RepositorySelector javadoc learns us that it's introduced in Log4j 1.2. RepositorySelector javadoc向我们学习它是在Log4j 1.2中引入的。 This would mean that there's a collision with another and older-versioned Log4j JAR file in the classpath which got precedence in classloading. 这意味着在类路径中与另一个和版本较旧的Log4j JAR文件发生冲突,该文件在类加载中优先。 It's likely somewhere hidden in one of the default classpaths of Weblogic. 它可能隐藏在Weblogic的一个默认类路径中。 I don't do Weblogic, but as hinted by others, you can also try to change the classloading order, if it's supported by Weblogic. 我不做Weblogic,但正如其他人暗示的那样,如果Weblogic支持,你也可以尝试更改类加载顺序。 Consult its documentation. 查阅其文档。

where myApp.LoggerStartupService is the class implemented from ApplicationLifecycleListener. 其中myApp.LoggerStartupService是从ApplicationLifecycleListener实现的类。

Ok, so you implemented a startup class. 好的,所以你实现了一个启动类。

I checked the classpath in my scripts and its set properly for all dependencies required for log4j. 我检查了我的脚本中的类路径及其设置,以适应log4j所需的所有依赖项。 Appenders & Categories are also there in the log4j.xml. log4j.xml中也有Appenders&Categories。

How did you setup that classpath? 你是如何设置该类路径的? How did you package your startup class and where did you put it? 你是如何打包你的创业课程的?你把它放在哪里? Also, where is log4j.jar , which one do you use? 另外, log4j.jar在哪里,你使用哪一个? Your startup class (and its dependencies) must be added to the server CLASSPATH . 必须将您的启动类(及其依赖项) 添加到服务器CLASSPATH Is this what you did? 这是你做的吗? Refer to Add startup and shutdown classes to the classpath in the official documentation for the details on how to do this. 有关如何执行此操作的详细信息,请参阅官方文档中的类路径添加启动和关闭类

I'd like to get confirmation of these points (but the final answer is that you should use a MANIFEST.MF in the jar of your startup class to reference third party libraries). 我想得到这些要点的确认(但最终的答案是你应该在启动类的jar中使用MANIFEST.MF来引用第三方库)。

For my case, I using log4j 2.x and cannot find org/apache/log4j/spi/RepositorySelector, because there is no such API in version 2.x, to use it, need add the dependency of log4j-1.2-api 对于我的情况,我使用log4j 2.x并且找不到org / apache / log4j / spi / RepositorySelector,因为版本2.x中没有这样的API,要使用它,需要添加log4j-1.2-api的依赖项

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>2.5</version>
    </dependency>

after

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.5</version>
    </dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM