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

Adding "originalFileName" token to FileNamePattern or TimeBasedRollingPolicy

    Details

      Description

      Let's consider the below example:

          <appender name="OutputInfo" class="ch.qos.logback.classic.sift.SiftingAppender">
              <filter class="ch.qos.logback.classic.filter.LevelFilter">
                  <level>ERROR</level>
                  <onMatch>ACCEPT</onMatch>
                  <onMismatch>DENY</onMismatch>
              </filter>
              <discriminator>
                  <key>automaticThreadName</key>
                  <defaultValue>Unnamed Thread</defaultValue>
              </discriminator>
              <discriminator>
                  <key>inputQueueName</key>
                  <defaultValue>APP</defaultValue>
              </discriminator>
              <discriminator>
                  <key>outputQueueName</key>
                  <defaultValue>APP</defaultValue>
              </discriminator>
              <sift>
                  <appender name="${automaticThreadName}" class="ch.qos.logback.core.rolling.RollingFileAppender">
                      <file>./LOGS/${inputQueueName}/${outputQueueName}/ERROR/${automaticThreadName}_ERROR_TODAY.log</file>
                      <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                          <Pattern>%d{yyyy-MM-dd HH:mm:ss} %mdc [%thread] %level %logger{35} - %msg%n</Pattern>
                      </encoder>
                      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                          <FileNamePattern>./LOGS/${inputQueueName}/${outputQueueName}/ERROR/ARCHIVE/%d{yyyyMMdd, aux}/${automaticThreadName}_%d{yyyyMMdd}_ERROR.zip</FileNamePattern>
                      </rollingPolicy>
                  </appender>
              </sift>
          </appender>
       

      It is not a valid example because FileNamePattern of TimeBasedRollingPolicy does not support MDC tokens like the SiftingAppender supports.

      This is causing a significant limitation for SiftingAppender log archiving - a diversity of file names of SiftingAppender can be archived only into a narrow list of archives due to TimeBasedRollingPolicy not supporting MDC tokens.

      This can be easily fixed just by adding 3 tokens into TimeBasedRollingPolicy FileNamePattern, which will represent:

      • %q - qualified file name of original physical file, e.g. ./logs/thread1/20181115/thread1_errors.log
      • %f - only file name of original physical file, e.g. "thread1_errors.log"
      • %p - only path of original physical file, e.g. "./logs/thread1/20181115/"

      Therefore the above config will look like:

          <appender name="OutputInfo" class="ch.qos.logback.classic.sift.SiftingAppender">
              <filter class="ch.qos.logback.classic.filter.LevelFilter">
                  <level>ERROR</level>
                  <onMatch>ACCEPT</onMatch>
                  <onMismatch>DENY</onMismatch>
              </filter>
              <discriminator>
                  <key>automaticThreadName</key>
                  <defaultValue>Unnamed Thread</defaultValue>
              </discriminator>
              <discriminator>
                  <key>inputQueueName</key>
                  <defaultValue>APP</defaultValue>
              </discriminator>
              <discriminator>
                  <key>outputQueueName</key>
                  <defaultValue>APP</defaultValue>
              </discriminator>
              <sift>
                  <appender name="${automaticThreadName}" class="ch.qos.logback.core.rolling.RollingFileAppender">
                      <file>./LOGS/${inputQueueName}/${outputQueueName}/ERROR/${automaticThreadName}_ERROR_TODAY.log</file>
                      <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                          <Pattern>%d{yyyy-MM-dd HH:mm:ss} %mdc [%thread] %level %logger{35} - %msg%n</Pattern>
                      </encoder>
                      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                          <FileNamePattern>%q.zip</FileNamePattern>
                      </rollingPolicy>
                  </appender>
              </sift>
          </appender>
      

        Attachments

          Activity

            People

            • Assignee:
              logback-dev Logback dev list
              Reporter:
              anton.pryamostanov Anton Pryamostanov
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: