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

Headers are getting added during every application startup while configuring programmatically

    XMLWordPrintable

Details

    Description

      Use case :

      Application has to write events into a ".tmp" file and there is a time based rolling policy which rotates log file for every minute.

      // code placeholder
      SizeAndTimeBasedRollingPolicy sizeAndTimeBasedRollingPolicy = new SizeAndTimeBasedRollingPolicy();
      sizeAndTimeBasedRollingPolicy.setContext(context);
      sizeAndTimeBasedRollingPolicy.setParent(csvAppender);
      sizeAndTimeBasedRollingPolicy.setMaxFileSize(FileSize.valueOf("5 MB"));
      sizeAndTimeBasedRollingPolicy.setFileNamePattern("my_test_csv-%d{yyyyMMddHHmm}-%i.csv");
      sizeAndTimeBasedRollingPolicy.start();
      return sizeAndTimeBasedRollingPolicy;
      

       

       

      Also, there is a header which needs to be appended for every log file. So I have used `PatternLayoutEncoderBase` class to set headers and pattern.

       

      // code placeholder
      public class HeaderLayoutEncoder extends PatternLayoutEncoderBase<ILoggingEvent> {
      
          private final String headers;
      
          public HeaderLayoutEncoder(LoggerContext context, String headers) {
              setContext(context);
              this.headers = headers;
          }
      
          @Override
          public void start() {
              PatternLayout patternLayout = new PatternLayout();
              patternLayout.setContext(context);
              patternLayout.setPattern("%msg%n");
              patternLayout.setFileHeader(headers);
              patternLayout.setOutputPatternAsHeader(false);
              patternLayout.start();
              this.layout = patternLayout;
              super.start();
          }
      }
      

       

       Problem: If I am restarting my application during the same minute, Headers are getting appended multiple times.

       

      So we need an option to not append the header if they are already present.

       

      I have also tried an option where we check the file during application startup, but there is a scenario where the application starts during a minute and the events are getting logged in the next minute. So If I do the below logic, I am missing headers for some files

       

      // code placeholder
      private String getFileHeader(String filePath, String headers) {
      
          File csvFilePath = new File(filePath);
      
          if (!csvFilePath.exists()) {
              return headers;
          }
      
          return
              Optional.of(csvFilePath)
                  .filter(csvFile -> {
                      try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
      
                          return
                              seq(br.lines())
                                  .noneMatch(l -> l.contains(headers));
                      } catch (IOException e) {
                          throw new IllegalStateException(e);
                      }
                  })
                  .map(file1 -> headers)
                  .orElse(null);
      }
      

       

      Attachments

        Activity

          People

            logback-dev Logback dev list
            saisrikarmutya Sai Srikar Mutya
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: