Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have an application that only runs when triggered by hardware. An legacy c++ application that we recently added log4cxx too, to generate logs to help debug rare production issues. We of course wanted daily logfiles.
It turns out that we never got rolling files.
To debug the problem we set it roll over on the minute rather than the day. We found that if the program was called within a few seconds from the top of the minute, the file would roll over. If it was called more than 5 seconds after the top of the minute, roll over did not occur.
In testing the program takes roughly 5 seconds to run.
Is there anyway to have log4 rollover the file when the program starts, if needed?
i.e. If we logged at minute 6 and then don't run again until minute 50, we'll roll over the log file before we starting logging for minute 50, rather than just appending to minute 6.
–
# Pattern to output the caller's file name and line number.
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.DatePattern=test-%Y-%m-%d.log
DailyRollingFileAppender
didn't work in my case too (at log4cxx 0.10.0), so I ended up using
RollingFileAppender
plus
TimeBasedRollingPolicy
:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="roll" class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="roll.%d{yyyy-MM-dd}.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p %t %c - %m%n"/>
</layout>
<param name="Append" value="true"/>
</appender>
<priority value="ALL"/>
<appender-ref ref="roll"/>
</root>
</log4j:configuration>
Point is not to specify <param name="file">
or it won't roll over (also it doesn't recognize StaticLogFileName
param as in the original log4j).
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.