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

Log4j.xml转Log4j2.xml遇问题:应用仍查找旧配置文件求助

Troubleshooting Log4j2 Upgrade Issue in Spring Boot

Hey there, let's work through this problem together. The fact that your app is still searching for log4j.xml and outputting old Log4j debug logs tells us the old Log4j library is still present in your classpath—even after you updated your dependencies. Here's how to fix this and address your custom JAR question:

1. Find and Remove Residual Old Log4j Dependencies

First, let's identify where the old Log4j is coming from. Run this Maven command to print your full dependency tree:

mvn dependency:tree

Look for any dependencies with groupId log4j (not org.apache.logging.log4j—that's Log4j2). These could be coming from:

  • Third-party libraries you're using
  • Your custom .jar files (which we'll cover next)

Once you find these, add exclusions for them in your pom.xml just like you did for Logback. For example, if a library pulls in old Log4j:

<dependency>
  <groupId>com.example</groupId>
  <artifactId>some-third-party-lib</artifactId>
  <exclusions>
    <exclusion>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
    </exclusion>
  </exclusions>
</dependency>

2. Handle Custom JARs Using Old Log4j

Yes, custom JARs that depend on old Log4j will cause conflicts—and this is likely part of your current problem. Here's how to fix it without rewriting the custom JAR:

  • Add the Log4j1-to-Log4j2 bridge dependency to your project. This lets old Log4j API calls route to Log4j2's implementation:
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-1.2-api</artifactId>
    </dependency>
    
  • If possible, long-term, update your custom JAR to use Log4j2 directly (or use SLF4J as an abstraction layer, which makes switching logging frameworks easier later).

Conflicts from old Log4j in custom JARs can lead to obvious errors like:

  • ClassNotFoundException for Log4j2 classes (if old Log4j takes precedence)
  • NoSuchMethodError (when old Log4j APIs are called that don't exist in Log4j2)
  • Your current issue: the app tries to load log4j.xml instead of log4j2.xml

3. Verify Spring Boot Configuration

Double-check your application config to make sure it's pointing to the right Log4j2 file:

  • In application.properties or application.yml, ensure you have:
    logging.config=classpath:log4j2.xml
    
  • For Spring Profiles, you can name your config files like log4j2-dev.xml, log4j2-prod.xml, etc. Spring Boot will automatically pick up the file matching your active profile (no extra config needed beyond activating the profile).

4. Clean and Rebuild

Sometimes cached dependencies or IDE state can cause issues. Do this to refresh everything:

  • Run mvn clean install -U to clean your build and force-update dependencies
  • Restart your IDE and clear its cache (in IntelliJ: File → Invalidate Caches...; in Eclipse: Project → Clean)

内容的提问来源于stack exchange,提问作者Max Mazur

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:58:16