Log4j仅在控制台输出日志但不生成日志文件问题求助
Hey there, let's work through why your Log4j setup isn't creating log files even though your project runs fine. I've gone through your configuration, and here are the most common fixes to try:
1. First, check the log directory exists and has write access
Your config points to D:/logs, but Log4j won't automatically create this folder unless you tell it to (more on that later).
- Head to your D drive and confirm the
logsfolder is there. If not, create it manually first to test. - Also, make sure the user running your project has write permissions for this folder. On Windows, right-click the folder → Properties → Security tab to verify. On Linux/macOS, use
chmodto adjust permissions if needed.
2. Confirm you're using Log4j 2 (not Log4j 1.x)
Your XML config uses Log4j 2's syntax (the <Configuration> root tag), so double-check your project dependencies:
- If you're using Maven/Gradle, ensure you've included Log4j 2's core and API dependencies, not the old Log4j 1.x jars. For Maven, it should look like this:
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>your-version-number</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>your-version-number</version> </dependency> - If you accidentally mixed Log4j 1.x and 2.x jars, the config won't load properly.
3. Make sure your code is using the correct Logger instance
You've set up a logger for org.project at debug level—confirm your code is fetching the right Logger:
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class YourServiceClass { // Use either the fully qualified name or the class itself private static final Logger logger = LogManager.getLogger("org.project"); // OR private static final Logger logger = LogManager.getLogger(YourServiceClass.class); }
If you're using SLF4J as a facade, add the log4j-slf4j-impl dependency to bridge logs to Log4j 2.
4. Let Log4j auto-create the log directory
If you're on Log4j 2.9 or newer, add the createDirectories="true" attribute to your RollingFile appender. This tells Log4j to make the D:/logs folder if it doesn't exist:
<RollingFile name="rollingFile" immediateFlush="true" fileName="${logHome}/mySample.log" filePattern="${logHome}/mySample-%i.log" createDirectories="true">
5. Enable Log4j internal debugging to spot issues
Temporarily change your config's status from warn to debug:
<Configuration status="debug">
When you run your project, Log4j will spit out detailed debug logs to the console. Look for lines like "Could not create directory" or "Appender rollingFile failed to start"—these will directly point to the problem.
6. Verify log levels aren't filtering your messages
Your Root logger is set to info level, and your org.project logger is debug—that's okay (child loggers can have lower levels than the root). But make sure you're actually logging messages at debug or higher in your code. For example:
logger.debug("This is a debug message"); // Should be logged logger.trace("This is a trace message"); // Won't be logged with your current config
Start with the first two checks—directory existence/permissions and dependency version—since those are the most frequent culprits. If those don't fix it, use the debug mode to get more clues!
内容的提问来源于stack exchange,提问作者Nitin Pareek

