/** * Returns a rotating file logger that logs the messages to files in the default directory. * * @throws FileNotFoundException if the current file cannot be opened or created. */ @Pure public static @Capturable @Nonnull RotatingFileLogger withDefaultDirectory() throws FileNotFoundException { return new RotatingFileLogger(); }
/** * Creates a rotating file logger that logs the messages to files in the default directory. * * @throws FileNotFoundException if the current file cannot be opened or created. */ protected RotatingFileLogger() throws FileNotFoundException { super(getCurrentFile()); this.currentDate = new Date(); Files.directory.register((configuration, oldFile, newFile) -> rotate()); }
/** * Rotates the log file. */ @Impure private void rotate() { this.currentDate = new Date(); try { setFile(getCurrentFile()); } catch (@Nonnull FileNotFoundException exception) { throw UncheckedExceptionBuilder.withCause(exception).build(); } }
@Impure @Override @SuppressWarnings("deprecation") protected void log(@Nonnull Level level, @Nonnull String caller, @Nonnull String thread, @Nonnull String message, @Nullable Throwable throwable) { final Date date = new Date(); if (date.getDate() != this.currentDate.getDate()) { rotate(); } super.log(level, caller, thread, message, throwable); }
/** * Initializes the logger with a rotating file logger. */ @PureWithSideEffects @Initialize(target = Logger.class) public static void initializeLogger() throws FileNotFoundException { if (Logger.logger.get() instanceof StandardOutputLogger) { Logger.logger.set(RotatingFileLogger.withDefaultDirectory()); Log.verbose("Replaced the default standard output logger with a rotating file logger."); } else { Log.verbose("Did not replace the non-default logger with a rotating file logger."); } }