Java log4j:ERROR “org.apache.log4j.ConsoleAppender”对象不可分配给“org.apache.log4j.Appender”变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21090735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:30:15  来源:igfitidea点击:

log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to a "org.apache.log4j.Appender" variable

javaexceptionlogginglog4jgradle

提问by Kasun Kariyawasam

I am making a web app using gradle. I used log4j in this app. When I build the project I am getting error...

我正在使用 gradle 制作一个网络应用程序。我在这个应用程序中使用了 log4j。当我构建项目时,我收到错误...

  • log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to a "org.apache.log4j.Appender" variable.
  • log4j:ERROR The class "org.apache.log4j.Appender" was loaded by
  • log4j:ERROR [org.powermock.core.classloader.MockClassLoader@16a40b2] whereas object of type
  • log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by [sun.misc.Launcher$AppClassLoader@5e3a78ad].
  • log4j:ERROR Could not instantiate appender named "stdout".
  • log4j:ERROR “org.apache.log4j.ConsoleAppender”对象不可分配给“org.apache.log4j.Appender”变量。
  • log4j:ERROR 类“org.apache.log4j.Appender”被加载
  • log4j:ERROR [org.powermock.core.classloader.MockClassLoader@16a40b2] 而对象类型
  • log4j:错误“org.apache.log4j.ConsoleAppender”由 [sun.misc.Launcher$AppClassLoader@5e3a78ad] 加载。
  • log4j:ERROR 无法实例化名为“stdout”的 appender。

I still did not find a good answer. And I forgot to say, My project build correctly and show log massages correctly. It doesn't work only on test classes.

我仍然没有找到一个好的答案。我忘了说,我的项目正确构建并正确显示日志信息。它不仅适用于测试类。

采纳答案by ToYonos

Adding this annotation works for me.

添加此注释对我有用。

@PowerMockIgnore("org.apache.log4j.*") 

回答by artplastika

Your classpath contains 2 copies of log4j library. Exclude one and try again.

您的类路径包含 2 个 log4j 库副本。排除一个,然后重试。

回答by Kasun Kariyawasam

There is a Clear answer here. :D

有一个明确的答案在这里。:D

When we have a final method and slf4j loggers in our classes. We have to use powermock in our test class. Because of this we have to use @MockPolicy(Slf4jMockPolicy.class) with particuler imports at the test class which we used Powermock.

当我们的类中有 final 方法和 slf4j 记录器时。我们必须在我们的测试类中使用 powermock。因此,我们必须在我们使用 Powermock 的测试类中使用 @MockPolicy(Slf4jMockPolicy.class) 和特定的导入。