Java logging framework guaranteeing log rotation at midnight

I am looking for a way to rotate the log file at midnight with the added requirement so that no matter what is written in the log, the rewind time MUST be respected (the functional equivalent would be the * nix logrotate program).

All implementations I've looked at (log4j, logback) require logging events as a trigger to rotate the log (the first logging event after 00:00 triggers the rotation). This means that there is no guarantee that the logs will be rotated at the specified time (since the required trigger event may occur several hours later).

Is there a logging system that guarantees the log rotation at the moment?


source to share

1 answer

If so, you can use CronTrigger to run a simple java class to create a dummy every day at midnight.

Assuming your code is in Springframework mode, you can use the below configuration as a reference.

<bean id="logRotateAlert" class="org.springframework.scheduling.quartz.JobDetailBean">
  <property name="jobClass">
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="logRotateAlert"/>
  <property name="cronExpression" value="0 0 0 * * ?"/>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
      <ref bean="cronTrigger"/>




All Articles