void scheduleDailyLoggingWorkInNextCheckTime ( ) { DailyRollingFileAppender dailyAppender = null ; Enumeration < ? > appenders = Logger . getRootLogger ( ) . getAllAppenders ( ) ; while ( appenders . hasMoreElements ( ) ) { Appender appender = ( Appender ) appenders . nextElement ( ) ; if ( appender instanceof DailyRollingFileAppender ) { dailyAppender = ( DailyRollingFileAppender ) appender ; } } final DailyRollingFileAppender dailyRollingFileAppender = dailyAppender ; Field field = null ; if ( dailyRollingFileAppender != null ) { try { field = dailyRollingFileAppender . getClass ( ) . getDeclaredField ( "nextCheck" ) ; field . setAccessible ( true ) ; } catch ( NoSuchFieldException e ) { hostLog . error ( "Failed to set daily system info logging: " + e . getMessage ( ) ) ; } } final Field nextCheckField = field ; long nextCheck = System . currentTimeMillis ( ) ; // the next part may throw exception, current time is the default value if ( dailyRollingFileAppender != null && nextCheckField != null ) { try { nextCheck = nextCheckField . getLong ( dailyRollingFileAppender ) ; scheduleWork ( new DailyLogTask ( ) , nextCheck - System . currentTimeMillis ( ) + 30 * 1000 , 0 , TimeUnit . MILLISECONDS ) ; } catch ( Exception e ) { hostLog . error ( "Failed to set daily system info logging: " + e . getMessage ( ) ) ; } } }
Get the next check time for a private member in log4j library which is not a reliable idea . It adds 30 seconds for the initial delay and uses a periodical thread to schedule the daily logging work with this delay .