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.
Solution 1: Migrate to Log4j 2.x (Highly Recommended)
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:
- Remove old Log4j 1.x dependencies
Delete any Log4j 1 entries in yourpom.xml, like this:<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> - Add Log4j 2 Spring Boot Starter
Include the official starter, and exclude the default Logback dependency from any existing starters (likespring-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> - 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:
New logger setup (don't forget to importprivate static final Logger logger = Logger.getLogger(YourClass.class);org.apache.logging.log4j.LogManager):private static final Logger logger = LogManager.getLogger(YourClass.class);
- Old import:
- Adjust your logging configuration
Convert your oldlog4j.propertiesfile to a Log4j 2 configuration (eitherlog4j2.xml,log4j2.properties, orlog4j2.yaml). Spring Boot will automatically pick up this file from yoursrc/main/resourcesfolder.
Solution 2: Stick with Log4j 1.x (Not Recommended)
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:
- Exclude the default Logback dependency from your starters (same as step 2 in Solution 1).
- Re-add Log4j 1.x dependency:
This will restore the missing classes, but again—this is a stopgap measure. Plan to migrate to Log4j 2 as soon as possible.<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
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

