从 JDK8 迁移到 JDK11 后,我收到“SLF4J:未找到 SLF4J 提供程序”并且未记录日志。 使用 Maven 和 IntelliJ。
输出:
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
在 JDK8 中,我在 pom.xml 中添加了这些内容,并且一切都运行良好。
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.7</version>
</dependency>
我环顾四周,按照提示添加了 slf4j18-impl 但我仍然遇到同样的问题。这就是它现在在 pom.xml 中的样子:
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j18-impl</artifactId>
<version>2.18.0</version>
</dependency>
这种代码在 JDK8 中可以运行,但在 JDK11 中不行
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestOutput {
private static final Logger LOG = LoggerFactory.getLogger(TestOutput.class);
public static void tryToLog() {
LOG.info("Hello world!");
}
}
只为 slf4j 添加一个依赖:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>