DailyRollingFileAppender not work
Solution 1
The org.apache.log4j.DailyRollingFileAppender will create new log file for each day, each hour or each minute but file name of the current log always will be in the format that you've specified in the "file" parameter. In your example it's "process.log". The file names of the logs for the previous hours will be in format "process.log.yyyy-MM-dd-HH".
Solution 2
It appears that if you're running windows this is a known bug:
see here: http://do.whileloop.org/2014/02/14/log4j-rolling-file-appenders-in-windows/
See here: https://issues.apache.org/bugzilla/show_bug.cgi?id=29726
And also here: http://www.coderanch.com/t/424837/java/java/Log-log-file-rolled-day
The solutions seems to be to use the extras here: http://logging.apache.org/log4j/extras/
Try using this appender with the correct policies defined:
http://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/RollingFileAppender.html
Related videos on Youtube
Comments
-
Sonrobby over 1 year
I use Log4j to write some log my program. I find and read many question and answer in this site, but i can't solve my problem.
Here my code:
1. log4j.xml
<appender name="rollingfileAppender" class="org.apache.log4j.DailyRollingFileAppender"> <param name="append" value="true"/> <param name="file" value="logs/process.log"/> <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss:SSS} %-5p [%c{1}] %m%n"/> </layout> </appender> <root> <level value="DEBUG"/> <appender-ref ref="rollingfileAppender"/> <appender-ref ref="stdout"/> </root>
2. My java code
package TestPacket; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; public class TestLog4jXML { static Logger logger = org.apache.log4j.Logger.getLogger(TestLog4jXML.class.getName()); public TestLog4jXML() { } public static void main(String[] args) { try { DOMConfigurator.configure("log4j1.xml"); logger.trace("Entering application."); logger.debug("Debug"); logger.info("info"); logger.warn("warn"); logger.error("error"); logger.fatal("fatal"); lungtng(); } catch (Exception e) { e.printStackTrace(); } } public static void lungtng() { logger.fatal("some text here"); logger.info("hello picaso"); } }
And i run my program, with eclipse, windows os. But the log file name only: process.log not in daily format: process.log.yyyy-MM-dd-HH
Who can explain this to me?
-
Sonrobby over 11 yearsyeb, i want to hourly logging for that. But nothing changes for every time i run this program. Could you copy my code and run it in your PC?
-
Isaac over 11 yearsActually this is wrong. By properly configuring
DailyRollingFileAppender
, you can have it rotate every minute as well. -
Artem Shafranov over 11 years@Isaac Oh, I see. Sorry for my foolishness. Didn't read about such behavior before. The name of the appender class is definitely misleading.
-
Artem Shafranov over 11 years@Sonrobby Your code works perfectly on my PC except for "stdout" appender that's missing.
-
Sonrobby over 11 yearsthank @Artem Shafranov so much, i've understood this problem.