Is it pure to get the Log4j RollingFileAppender to ride shortly after midnight?

The normal behavior of the Log4j RollingFileAppender is to roll when the first log message occurs on another day, but some of them feel warm and fuzzy with empty log files for each date, even if nothing happened. Is there a way to make it roll after midnight without writing dummy messages to the log?

+3
source share
1 answer

I examined this code very carefully - the simple answer is no. A rollover starts as part of the doAppend () stream in Appender - the only way to call it is to register something.

You can fake this with cron: just cron script tap the file for tomorrow, like 11:58. This will help you find the right type of log file.

Here's the code that implements the rollover function:

void rollOver() throws IOException {

    /* Compute filename, but only if datePattern is specified */
    if (datePattern == null) {
      errorHandler.error("Missing DatePattern option in rollOver().");
      return;
    }

    String datedFilename = fileName+sdf.format(now);
    // It is too early to roll over because we are still within the
    // bounds of the current interval. Rollover will occur once the
    // next interval is reached.
    if (scheduledFilename.equals(datedFilename)) {
      return;
    }

    // close current file, and rename it to datedFilename
    this.closeFile();

    File target  = new File(scheduledFilename);
    if (target.exists()) {
      target.delete();
    }

    File file = new File(fileName);
    boolean result = file.renameTo(target);
    if(result) {
      LogLog.debug(fileName +" -> "+ scheduledFilename);
    } else {
      LogLog.error("Failed to rename ["+fileName+"] to ["+scheduledFilename+"].");
    }

    try {
      // This will also close the file. This is OK since multiple
      // close operations are safe.
      this.setFile(fileName, false, this.bufferedIO, this.bufferSize);
    }
    catch(IOException e) {
      errorHandler.error("setFile("+fileName+", false) call failed.");
    }
    scheduledFilename = datedFilename;
  }
+4
source

Source: https://habr.com/ru/post/1772751/


All Articles