Log4J 2与RabbitMQ及Spring Boot集成异常求助
Hey there, let's work through your Log4J 2 issue step by step. I've reviewed your pom.xml snippet, and here are targeted checks and fixes to get your logging up and running:
1. Verify Your Dependency Tree for Conflicting Logging Bindings
The most common culprit here is hidden dependencies pulling in a different SLF4J binding (like Logback) that overrides Log4J 2. Run this Maven command to spot conflicts:
mvn dependency:tree | grep -E "(logging|slf4j)"
Look for entries like logback-classic or slf4j-simple—these need to be excluded from any dependencies that include them. For example, if you have other starters (like spring-boot-starter-web), you'll need to exclude spring-boot-starter-logging from those too:
<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>
2. Ensure Your Log4J 2 Configuration File is Correctly Placed and Formatted
Spring Boot 1.5.x looks for Log4J 2 config files in the src/main/resources directory by default. Valid filenames are log4j2.xml, log4j2.json, or log4j2.yml. Here's a minimal working XML config to test with:
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="INFO"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration>
If you're using YAML for config, you'll need to add this dependency (Spring Boot 1.5.x doesn't include it by default):
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> </dependency>
3. Explicitly Configure Log4J 2 in Spring Boot
Add this line to your application.properties or application.yml to force Spring Boot to use your Log4J 2 config:
logging.config=classpath:log4j2.xml
Also, check that no JVM startup parameters (like -Dlogging.config) are overriding this setting.
4. Validate Log4J 2 Compatibility with Spring Boot 1.5.3
Spring Boot 1.5.3 is tied to Log4J 2 version 2.7. To avoid version mismatches, explicitly set the version in your pom.xml properties:
<properties> <log4j2.version>2.7</log4j2.version> <!-- Your existing properties --> </properties>
5. Test Logging with a Direct Log4J 2 Instance
Add a test log statement in your main application class to rule out code-level issues:
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TestProjectApplication { private static final Logger logger = LogManager.getLogger(TestProjectApplication.class); public static void main(String[] args) { SpringApplication.run(TestProjectApplication.class, args); logger.info("=== Log4J 2 Test Message ==="); logger.debug("=== Debug Message (check if level is enabled) ==="); } }
If these messages don't appear in your console, go back to step 1—you almost certainly have a conflicting logging binding.
6. Check Spring Boot's Startup Logs
When your app starts, look for a line like this in the console:
Logging system initialized using 'org.apache.logging.log4j.core.LoggerContext' adapter
If you see a reference to Logback instead, that confirms a hidden Logback dependency is still active.
内容的提问来源于stack exchange,提问作者Aditya Ekbote

