Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-1600

Re-create log if it gets deleted at runtime on Unix/Linux

    XMLWordPrintable

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • 1.2.0
    • logback-core
    • None
    • Unix/Linux

    Description

      in logback version 1.1.2~1.1.9, it re-creates log files when get deleting at runtime on Unix/Linux.  It works normally.

       

      I used logback version 1.1.2,  and I saw this article and applied this my project using logback. 

      https://jira.qos.ch/browse/LOGBACK-1004

         

      But, I upgraded version 1.1.2 -> 1.2.9 and it did not re create log files when it gets deleted at runtime on Unix/Linux.  

       

      I think it's because FileAppender class's  writeOut method not invoked. 

      FileAppender class's writeOut method overrieds OutputStreamAppender class's writeOut method. 

      I saw this news  from the logback's news. 

      February 8th, 2017, Release of version 1.2.0

      Encoder interface has changed and is no longer expected to handle an OutputStream.

      Encoder interface has changed and is no longer expected to handle an OutputStream. This simplification allows finer-grain locking resulting in significantly improved performance.

       

      Exactly, from logback version 1.2.0, it did not Re-create log if it gets deleted at runtime on Unix/Linux, because FileAppender class's  writeOut method not invoked.  

       

      I don't know why it is. 

       this is my code's segment.  I don't use logback.xml.  

       

      Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

      LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory()

      TimeBasedRollingPolicy<ILoggingEvent> timePolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
      timePolicy.setContext(context);

      WorldWritableFileAppender<ILoggingEvent> rollingFileAppender = new WorldWritableFileAppender<ILoggingEvent>();

      timePolicy.setParent(rollingFileAppender);
      timePolicy.setFileNamePattern(logDir + "/" + fileNamePattern + "-" + product + "_%i.log." + compressFormat);
      timePolicy.setMaxHistory(maxHistory);
      timePolicy.setCleanHistoryOnStart(false);
      timePolicy.start();

      SizeAndTimeBasedFNATP<ILoggingEvent> sizePolicy = new SizeAndTimeBasedFNATP<ILoggingEvent>();
      sizePolicy.setContext(context);
      sizePolicy.setMaxFileSize(FileSize.valueOf(maxFileSize));
      sizePolicy.setTimeBasedRollingPolicy(timePolicy);
      sizePolicy.start();

      timePolicy.setTimeBasedFileNamingAndTriggeringPolicy(sizePolicy);
      timePolicy.start();

      rollingFileAppender.setTriggeringPolicy(sizePolicy);
      rollingFileAppender.setRollingPolicy(timePolicy);

      PatternLayoutEncoder pl = new PatternLayoutEncoder();
      pl.setContext(context);
      pl.setPattern(pattern);
      pl.start();

      rollingFileAppender.setEncoder(pl);
      rollingFileAppender.start();

      root.addAppender(rollingFileAppender);

       

       

      WorldWritableFileAppender class is custom class and extends RollingFileAppender class. 

      writeOut method overrides FileAppender class's  writeOut method.  and it's not invoked.  

       

      public class WorldWritableFileAppender<E> extends RollingFileAppender<E>{
      private File file_ptr;

      @Override
      public void openFile(String file_name) throws IOException {
      if(file_name != null) {
      File file = new File(file_name);
      file_ptr = file;

      if (FileUtil.createMissingParentDirectories(file))

      { ...}

      super.openFile(file_name);

      if(file_name != null)

      { File f = new File(file_name); file_ptr = f; if (f.exists()) \{...}

        }
      }

      // writeOut method is not invoked at runtime on Linux/Unix

      @Override
      protected void writeOut(E event) throws IOException {

      if(file_ptr == null)

      { file_ptr = new File(getFile()); }

      if(!file_ptr.exists())

      { super.openFile(getFile()); }

      super.writeOut(event);
      }
      }

       

      Attachments

        Activity

          People

            logback-dev Logback dev list
            junmoKang junmoKang
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: