Java 错误状态记录器Log4j2找不到日志记录实现
我试图实现log4j2,但它一直抛出以下错误Java 错误状态记录器Log4j2找不到日志记录实现,java,eclipse,log4j2,Java,Eclipse,Log4j2,我试图实现log4j2,但它一直抛出以下错误 > ERROR StatusLogger Log4j2 could not find a logging implementation. > Please add log4j-core to the classpath. Using SimpleLogger to log to > the console... > ERROR LogExample This Will Be Printed On Error > F
> ERROR StatusLogger Log4j2 could not find a logging implementation.
> Please add log4j-core to the classpath. Using SimpleLogger to log to
> the console...
> ERROR LogExample This Will Be Printed On Error
> FATAL LogExample This Will Be Printed On Fatal
我试过网上给出的解决方案。但这似乎对我不起作用
这是我试图运行的代码
package demo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LogExample {
private static final Logger LOG = LogManager.getLogger(LogExample.class);
public static void main(String[] args) {
LOG.debug("This Will Be Printed On Debug");
LOG.info("This Will Be Printed On Info");
LOG.warn("This Will Be Printed On Warn");
LOG.error("This Will Be Printed On Error");
LOG.fatal("This Will Be Printed On Fatal");
LOG.info("Appending string: {}.", "Hello, World");
}
}
在pom.xml中添加的项目和依赖项:
非常感谢您的帮助。通过设置log4j2配置文件的系统属性解决了错误消息。下面是下面的代码
System.setProperty("log4j.configurationFile","./path_to_the_log4j2_config_file/log4j2.xml");
Logger log = LogManager.getLogger(LogExample.class.getName());
为了解决Spring Boot的类似问题,我添加了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
org.springframework.boot
spring-boot-starter-log4j2
对于我的POM,此依赖关系有助于避免lambda出现此错误。
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.8.2</version>
</dependency>
org.apache.logging.log4j
log4j-to-slf4j
2.8.2
在我的例子中,错误与maven shade插件版本(3.2.2)有关
当我将这个插件设置为3.2.1版时,它就可以工作了。如果你已经在maven pom中添加了log4j内核,那么你就不需要更多它应该可以工作的东西了。问题可能是您的IDE没有看到您的maven依赖性。尝试重新加载或重新导入maven依赖项,或者重新启动IDE也会有所帮助。您需要将log4j core jar添加到类路径中。我假设您使用的是maven,因此基本上可以通过创建一个包含所有依赖项的jar(而不是只包含代码的jar)来实现这一点。你可以看一下Maven HuffelPuto来做。如果你用另一种方式来解决它,请考虑接受其中一个答案,或者你自己的答案。谢谢提醒。我找到了另一种方法,将其作为另一个答案发布。非常感谢您的帮助。@cody.tv.weber能否提供更多详细信息或您遇到的错误。只需重新检查您是否遵循了所有步骤。@Alok log4j.configurationFile是否已弃用?@gaurav您所说的弃用是什么意思?log4j2是log4j的升级版本。与以前的版本不同,log4j2不支持将.properties文件作为配置文件。@Alok我没有说log4j2不推荐使用。我说过属性“log4j.configurationFile”已被弃用。建议使用“log4j2.configurationFile”