Log4j.xml转Log4j2.xml遇问题:应用仍查找旧配置文件求助
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
.jarfiles (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:
ClassNotFoundExceptionfor 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.xmlinstead oflog4j2.xml
3. Verify Spring Boot Configuration
Double-check your application config to make sure it's pointing to the right Log4j2 file:
- In
application.propertiesorapplication.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 -Uto 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

