You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Eclipse Maven项目中使用Log4j2遇报错:找不到日志实现

问题:Log4j2提示找不到logging implementation,但已添加log4j-core依赖

我使用maven-archetype-quickstart创建了一个新Maven项目,开发环境是Ubuntu 17.10上的Eclipse Java EE IDE for Web Developers Oxygen.3a Release(4.7.3a)。

我的POM.xml配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.rogatio</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>helloworld</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.11.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.11.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <systemProperties>
            <property>
              <name>log4j.configurationFile</name>
              <value>log4j2.properties</value>
            </property>
          </systemProperties>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

主类代码:

package org.rogatio.helloworld;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class App {
 final static Logger logger = LogManager.getLogger(App.class);
 public static void main(String[] args) {
 logger.info("App started.");
 System.out.println("Hello World!");
 sayHi();
 }
 public static String sayHi() {
 logger.info("Hi!");
 System.out.println("Hi!");
 return "Hi!";
 }
}

测试类代码:

package org.rogatio.helloworld;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class AppTest extends TestCase {
 /**
 * Create the test case
 *
 * @param testName name of the test case
 */
 public AppTest( String testName ) {
 super( testName );
 }
 /**
 * @return the suite of tests being tested
 */
 public static Test suite() {
 return new TestSuite( AppTest.class );
 }
 public void testApp() {
 assertTrue( true );
 }
 public void testHi() {
 assertEquals( App.sayHi(), "Hi!" );
 }
}

项目结构如下:
Project Filestructure

运行Maven测试时抛出错误:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.rogatio.helloworld.AppTest
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Hi!
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.233 sec - in org.rogatio.helloworld.AppTest

运行主程序时同样报错:

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Hello World!
Hi!

我已配置相关内容且资源在类路径中,不清楚问题所在,请问是POM配置有误吗?


解决方案

这个错误看起来矛盾,但本质上大多是依赖未正确加载或类路径缓存问题导致的,我们一步步排查解决:

1. 强制更新Maven依赖

Eclipse有时会缓存旧的依赖状态,导致新添加的log4j-core没有被正确导入:

  • 右键项目 → 选择Maven → Update Project...
  • 在弹出窗口勾选Force Update of Snapshots/Releases,点击OK等待更新完成
  • 更新后检查项目的Maven Dependencies目录,确认log4j-core-2.11.0.jar是否存在

2. 清理并重新构建项目

Maven的target目录可能存在缓存的编译文件,导致依赖未正确打包:

  • 在Eclipse中右键项目 → Run As → Maven Clean,清理target目录
  • 接着执行Run As → Maven Install,重新编译打包项目
  • 之后再运行测试或主程序,查看错误是否消失

3. 验证依赖树确认依赖存在

执行mvn dependency:tree命令(可在Eclipse的Run As → Maven Build...中输入该目标),检查输出是否包含以下条目:

[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.11.0:compile

如果没有这个条目,说明Maven未成功拉取log4j-core依赖,可能是网络问题或Maven仓库配置异常,需要检查settings.xml是否能访问中央仓库。

4. 更新Maven Surefire插件版本

你当前使用的maven-surefire-plugin版本2.17较为老旧,可能与Log4j2存在兼容性问题,建议更新到最新稳定版(如3.2.5):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.5</version>
    <configuration>
        <systemProperties>
            <property>
                <name>log4j.configurationFile</name>
                <value>log4j2.properties</value>
            </property>
        </systemProperties>
    </configuration>
</plugin>

5. 确认log4j2配置文件位置

确保log4j2.properties放在src/main/resources目录下(测试环境可放在src/test/resources),这样Maven才会将其复制到类路径中。虽然你的错误提示是找不到实现,但配置文件位置错误也可能引发初始化异常,建议一并确认。

按照以上步骤操作后,应该就能解决Log4j2找不到实现的问题了。


内容的提问来源于stack exchange,提问作者Matthias Wegner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:17:33