使用Tomcat与Log4j时每日日志文件停止生成的技术求助
Hey Leo, sorry to hear your daily logs stopped rolling—let’s break down the most common issues and fixes for this scenario with Tomcat and Log4j 1.x:
Key Checks to Perform
1. Verify Directory Permissions
First, make sure the Tomcat runtime user has read/write/execute permissions on the logs/debuglog/ directory.
- On Linux/Unix, run this command to check access:
Ensure the user running Tomcat (usuallyls -ld logs/debuglog/tomcator your system user) hasrwxpermissions here. - On Windows, right-click the folder → Properties → Security tab, confirm the Tomcat service user has modify permissions.
2. Complete Your DailyRollingFileAppender Configuration
Looking at your provided config, you’re missing the layout setup for the dailyfile appender—this can cause Log4j to fail initializing the appender silently. Add these lines to your properties:
log4j.appender.dailyfile.layout=org.apache.log4j.PatternLayout log4j.appender.dailyfile.layout.ConversionPattern=%-5p %c %M:%L %m%n
Without a valid layout, the appender won’t write any logs at all.
3. Check Version Compatibility & Known Bugs
Log4j 1.x’s DailyRollingFileAppender has known bugs in older releases (e.g., date parsing errors, time zone mismatches, file locking issues).
- Ensure you’re using the latest stable Log4j 1.x version (1.2.17) in your app’s
WEB-INF/libdirectory. - If you’re running a newer JDK (1.8+), some older Log4j versions might have compatibility gaps that break rolling logic.
4. Look for Initialization Errors
Check Tomcat’s core logs for Log4j startup failures:
- On Linux, this is typically
catalina.outin the Tomcat logs directory. - On Windows, check
localhost.logorcatalina.log.
Watch for lines like:
ERROR log4j: Failed to activate appender named [dailyfile].
These messages will directly tell you why the appender isn’t working (e.g., permission denied, missing layout, invalid date pattern).
5. Validate the Date Rolling Pattern
By default, DailyRollingFileAppender uses .yyyy-MM-dd as the date pattern, but explicitly setting it can avoid timezone or parsing glitches. Add this line to your config:
log4j.appender.dailyfile.DatePattern='.'yyyy-MM-dd
Also confirm your server’s system time and timezone are correct—if the time jumps backward (e.g., due to NTP sync), Log4j might skip rolling to avoid overwriting existing files.
6. Rule Out Log Framework Conflicts
Tomcat uses java.util.logging (JUL) by default, which can conflict with Log4j if not properly isolated:
- Make sure your web app only includes the Log4j 1.x JAR in
WEB-INF/lib(no conflicting SLF4J bridges or other logging frameworks unless explicitly configured). - Check if Tomcat’s
conf/logging.propertiesis overriding Log4j’s behavior—you might need to disable JUL for your app or use a bridge library.
7. Test Manual Rolling
In a test environment, try these steps to trigger rolling:
- Restart Tomcat and check if a new log file (with the current date suffix) is created.
- Manually advance the system date by one day, then write a debug log entry—verify if a new rolled file is generated.
If none of these steps resolve the issue, share any error messages from catalina.out or enable temporary Log4j debug logging (add log4j.debug=true to your properties file) for deeper troubleshooting.
内容的提问来源于stack exchange,提问作者Leo

