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

Spring Boot 1.x升级至2.x时遭遇Log4j编译错误求助

Hey there, let's fix that log4j compilation error you're facing after upgrading your Spring Boot app from 1.x to 2.x! This is a common issue when making this jump, so let's break down why it's happening and how to resolve it properly.

Why This Error Happens

  • Spring Boot 2.x defaults to Logback: Unlike Spring Boot 1.x, which had more flexible log framework support, Spring Boot 2.x uses Logback as its default logging implementation. It no longer includes Log4j 1.x dependencies out of the box.
  • Log4j 1.x is end-of-life: Apache stopped maintaining Log4j 1.x years ago, and it has known security vulnerabilities. Spring Boot 2.x actively discourages its use, which is why the dependencies are missing.
  • Missing dependencies: After upgrading, your code references org.apache.log4j.* classes that aren't present in your project's classpath anymore, leading to compilation errors.

Log4j 2 is the secure, modern successor to Log4j 1.x, and it integrates seamlessly with Spring Boot 2.x. Here's how to make the switch:

  1. Remove old Log4j 1.x dependencies
    Delete any Log4j 1 entries in your pom.xml, like this:
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    
  2. Add Log4j 2 Spring Boot Starter
    Include the official starter, and exclude the default Logback dependency from any existing starters (like spring-boot-starter-web):
    <!-- Exclude Logback from web starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <!-- Add Log4j 2 starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    
  3. Update your code imports and logger initialization
    Replace old Log4j 1.x references with Log4j 2 equivalents:
    • Old import: import org.apache.log4j.Logger;
      New import: import org.apache.logging.log4j.Logger;
    • Old logger setup:
      private static final Logger logger = Logger.getLogger(YourClass.class);
      
      New logger setup (don't forget to import org.apache.logging.log4j.LogManager):
      private static final Logger logger = LogManager.getLogger(YourClass.class);
      
  4. Adjust your logging configuration
    Convert your old log4j.properties file to a Log4j 2 configuration (either log4j2.xml, log4j2.properties, or log4j2.yaml). Spring Boot will automatically pick up this file from your src/main/resources folder.

If you need a temporary fix before migrating, you can re-add Log4j 1.x dependencies, but note that this is unsafe long-term due to security vulnerabilities:

  1. Exclude the default Logback dependency from your starters (same as step 2 in Solution 1).
  2. Re-add Log4j 1.x dependency:
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    
    This will restore the missing classes, but again—this is a stopgap measure. Plan to migrate to Log4j 2 as soon as possible.

Final Notes

Migrating to Log4j 2 isn't just about fixing compilation errors—it's about keeping your app secure and taking advantage of better performance and features that Log4j 2 offers. It's the right long-term fix for Spring Boot 2.x.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:10:18