Log4j2日志无法写入文件问题排查求助
Let's troubleshoot your Log4j2 file logging issue step by step—here are the key areas to check based on your setup:
1. Manual Log4j Initialization Clashes with Spring Boot
Your code calls Configurator.initialize("hzServer","log4j2.xml") manually, but Spring Boot handles log system initialization automatically during startup. This manual call can disrupt Spring's logging setup, leading to your log4j2.xml configuration not being applied properly.
Fix: Remove that line entirely. Spring Boot will automatically pick up log4j2.xml if it’s placed in src/main/resources.
2. Logger Package Mismatch or Missing Root Logger
You’ve configured a Logger for com.example, but your HzServer class might not be in that package. If your class lives in a different package (e.g., com.yourcompany), this Logger won’t capture its logs at all.
Quick Fix: Add a Root Logger to your log4j2.xml to ensure all logs are routed to your appender, regardless of package:
<Loggers> <Logger name="com.example" level="debug" includeLocation="true" additivity="false"> <AppenderRef ref="HzServer" /> </Logger> <!-- Add this Root Logger --> <Root level="debug"> <AppenderRef ref="HzServer"/> </Root> </Loggers>
Or, if you want to target only your app’s package, update the Logger’s name to match your HzServer class’s actual package.
3. immediateFlush="false" Might Be Holding Logs in Buffer
You set immediateFlush="false" on your RollingRandomAccessFile appender. This means logs are stored in memory buffers and only written to disk when the buffer fills up or the program exits cleanly. If your Spring Boot app is still running, you won’t see logs in the file yet.
Test: Temporarily set immediateFlush="true" to force logs to write immediately, then check the file again.
4. Spring Boot’s Default Logback Dependency Conflict
Spring Boot uses Logback by default. Even though you added Log4j2 dependencies, Logback might still be loading first, overriding your Log4j2 setup.
Fix: Exclude Logback from your Spring Boot starter and use the official Log4j2 starter for compatibility:
<!-- Exclude Logback from the default starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- Add Log4j2 starter (match version to your Spring Boot version) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> <!-- For example, if using Spring Boot 2.1.x, use version 2.1.17.RELEASE --> </dependency>
Also, note that your Log4j2 version (2.8.2) is quite old—make sure it’s compatible with your Spring Boot version. Newer Spring Boot releases require newer Log4j2 versions to avoid compatibility bugs.
5. Logger Initialization Order Issue
Your code initializes the LoggerContext before Spring’s dependency injection completes. This can lead to using an unconfigured Logger instance. Stick to Spring’s managed logging by only using LoggerFactory.getLogger(HzServer.class) without manual context setup.
Try these fixes one by one, starting with removing the manual initialization and adding the Root Logger—those are the most likely culprits.
内容的提问来源于stack exchange,提问作者Tatkal

