无法在 Java 项目中加载类“org.slf4j.impl.StaticLoggerBinder”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16190880/
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-16 05:42:03  来源:igfitidea点击:

Failed to load class "org.slf4j.impl.StaticLoggerBinder" error in java project?

javalog4j

提问by user2210071

I'm getting Failed to load class "org.slf4j.impl.StaticLoggerBinder"error.I want to write the logger in to a file.so i used log4j.jar and am using apache tomcat server.

我收到Failed to load class "org.slf4j.impl.StaticLoggerBinder"错误。我想将记录器写入文件。所以我使用了 log4j.jar 并且正在使用 apache tomcat 服务器。

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

回答by Dima

you need to add the slf4j jar or dependency to your project

您需要将 slf4j jar 或依赖项添加到您的项目中

回答by vijayk

Please import slf4j-api-1.6.6.jar for your project. 1.write following code in your java class

请为您的项目导入 slf4j-api-1.6.6.jar。1.在你的java类中编写以下代码

private Logger logger = LoggerFactory.getLogger(this.getClass());

2.import following:

2.导入如下:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

3.now you can use logger.info(); to print anything.

3.现在你可以使用logger.info(); 打印任何东西。

回答by Konstantinos Margaritis

First of all. Regarding the dependencies.

首先。关于依赖关系。

In order to add SLF4J you must put ONEand only ONEof these dependencies in your pom.xml. It depends on what implementation you choose to use. Every dependency you add in the pom.xml is added automatically in the classpath. If one of the below dependencies are provided by another dependency then you can omit it. Dont forget that you must include only one even if the dependency is provided by another dependency. Notice that i have omitted the version from the dependencies. Use the latest available.

为了添加 SLF4J,您必须在 pom.xml 中放置一个且仅一个这些依赖项。这取决于您选择使用什么实现。您在 pom.xml 中添加的每个依赖项都会自动添加到类路径中。如果以下依赖项之一由另一个依赖项提供,则您可以省略它。不要忘记,即使依赖项是由另一个依赖项提供的,您也必须只包含一个。请注意,我从依赖项中省略了版本。使用最新可用的。

<dependency>
   <groupId>ch.qos.logback</groupId>
   <artifactId>logback-classic</artifactId>
   <version></version>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-simple</artifactId>
   <version></version>
   <scope>compile</scope>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version></version>
   <scope>compile</scope>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-jdk14</artifactId>
   <version></version>
   <scope>compile</scope>
</dependency>

Now regarding the annoying error you are getting when building your maven project. If after having only one of the above dependencies you still get the SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". then you are facing a bug from m2e.

现在关于您在构建 Maven 项目时遇到的烦人错误。如果在只有上述依赖项之一后,您仍然得到 SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。那么您正面临来自 m2e 的错误。

Eclipse Juno and Indigo, when using the bundled maven version(m2e), are not suppressing the message SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". This behaviour is present from the m2e version 1.1.0.20120530-0009 and onwards.

Eclipse Juno 和 Indigo 在使用捆绑的 maven 版本 (m2e) 时不会抑制消息 SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。此行为存在于 m2e 版本 1.1.0.20120530-0009 及更高版本。

Although, this is indicated as an error your logs will be saved normally. The highlighted error will still be present until there is a fix of this bug. More about this in the m2e support site.

虽然,这表示为错误,您的日志将被正常保存。在修复此错误之前,突出显示的错误仍将存在。在m2e 支持站点中了解更多信息。

The current available solution is to use an external maven version rather than the bundled version of Eclipse. You can find about this solution and more details regarding this bug in the question below which i think describes the same problem you are facing.

当前可用的解决方案是使用外部 maven 版本而不是 Eclipse 的捆绑版本。您可以在下面的问题中找到有关此解决方案以及有关此错误的更多详细信息,我认为该问题描述了您面临的相同问题。

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". error

SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。错误

回答by Stepan Dyatkovskiy

There is no explicit example of how to use log4j and sl4j together in the manual. But there are plenty of unofficial ones, I like this one: http://www.javavillage.in/slf4j-with-log4j.php

手册中没有明确的示例说明如何一起使用 log4j 和 sl4j。但是有很多非官方的,我喜欢这个:http: //www.javavillage.in/slf4j-with-log4j.php

Noteonly two maven deps. There is no explicit log4j dep, it is loaded automatically.

请注意只有两个 maven dep。没有明确的 log4j dep,它是自动加载的。

Also(and it costed me 1 hour and 14 minutes) some versions of sl4j are not stable, and it matters. Version 1.8-beta (yeah.. beta) doesn't work. So I used 1.7.13 and it works.

此外(它花费了我 1 小时 14 分钟)某些版本的 sl4j 不稳定,这很重要。版本 1.8-beta(是的.. beta)不起作用。所以我使用了 1.7.13 并且它有效。

So, check for maven deps, it should be like this:

所以,检查maven deps,它应该是这样的:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.13</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.13</version>
</dependency>

Default configuration location is resources folder. E.g. for main sources:

默认配置位置是资源文件夹。例如对于主要来源:

project/src/main/resources

or for tests:

或用于测试:

project/src

Cheers!

干杯!