I'm looking for a way to rotate a log file at midnight with the added requirement that regardless of what gets written in the log, the roll over time MUST be respected (the functional equivalent would be the *nix logrotate program).
All implementations I've looked at (log4j, logback) require a logging event as a trigger for a log rotation (the first log event after 00:00 triggers the rotation). This means there is no guarantee that logs are rotated at a given time (as the required trigger event might arrive hours later).
Is there a logging framework that guarantees log rotation at a given time?
If it is so, you can use CronTrigger to trigger a simple java class to do a dummy log at midnight everyday.
Assuming your code is on springframework, you can take the below configuration as a reference.
<bean id="logRotateAlert" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>xxx.xxx.LogRotateAlert</value>
</property>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="logRotateAlert"/>
<property name="cronExpression" value="0 0 0 * * ?"/>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With