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

Cleaning old log files created by Sifting appender child

    XMLWordPrintable

Details

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • None
    • None
    • None

    Description

      I have SiftingAppender which creates logs per each job run. I need to automatically clean log files which are 2 weeks old. I tried to use RollingAppender, but it does not work since each log file name has unique job run id. This is what I have tired so far:

      <configuration>
        <!-- To see the errors generated by SiftingAppender-->
        <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
      
        <appender name="JOB_LOG" class="ch.qos.logback.classic.sift.SiftingAppender">
      
          <!-- This is MDC value -->
          <!-- We will assign a value to 'jobRunId' via Java code -->
          <discriminator>
            <key>jobRunId</key>
            <defaultValue>null</defaultValue>
          </discriminator>
      
          <!-- This is filter to deny log events which mdc value for 'jobRunId' equals 'null'.  -->
          <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator
                class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
              <expression>
                mdc.get("jobRunId") == null
              </expression>
            </evaluator>
            <OnMismatch>NEUTRAL</OnMismatch>
            <OnMatch>DENY</OnMatch>
          </filter>
      
          <sift>
      
            <appender name="FILE-${jobRunId}" class="ch.qos.logback.core.rolling.RollingFileAppender">
              <file>logs/jobs/${jobRunId}.log</file>
              <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%level] [%logger{60}] - %msg%n</pattern>
              </encoder>
      
              <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- Set to minutes for testing purposes. TODO: change to daily rollover. -->
                <fileNamePattern>logs/jobs/%d{yyyy-MM-dd_HH:mm:ss}.${jobRunId}.log</fileNamePattern>
                <!--  Set to 5 seconds for testing purposes. TODO: change to 14 days. -->
                <maxHistory>5</maxHistory>
                <cleanHistoryOnStart>true</cleanHistoryOnStart>
              </rollingPolicy>
      
            </appender>
      
          </sift>
      
        </appender>
      
        <root level="TRACE">
          <appender-ref ref="JOB_LOG"/>
        </root>
      </configuration>
      

       
      Console log suggests that files have been archived. But clean on start does not delete them I would assume since each appender name is dynamic in my case:

       15:18:15,194 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
      15:18:15,197 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE-aFuq3Q8H96]
      15:18:15,204 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
      15:18:15,228 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
      15:18:15,230 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern logs/jobs/%d{yyyy-MM-dd_HH:mm:ss}.aFuq3Q8H96.log for the active file
      15:18:15,234 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd_HH:mm:ss' from file name pattern 'logs/jobs/%d{yyyy-MM-dd_HH:mm:ss}.aFuq3Q8H96.log'.
      15:18:15,234 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over every second.
      15:18:15,234 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Thu Oct 25 15:18:15 PDT 2018
      15:18:15,235 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Cleaning on start up
      15:18:15,235 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - first clean up after appender initialization
      15:18:15,235 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - periodsElapsed = 336
      15:18:15,257 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE-aFuq3Q8H96] - Active log file name: /logs/jobs/aFuq3Q8H96.log
      15:18:15,258 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE-aFuq3Q8H96] - File property is set to [/logs/jobs/aFuq3Q8H96.log]
      15:18:15.193 INFO  c.l.t.logging.testing.LogbackTest - Hello, I am MDC
      15:18:17.261 INFO  c.l.t.logging.testing.LogbackTest - Hello, I am not MDC
      15:18:17,262 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
      15:18:17,262 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE-CjA2HVpgTq]
      15:18:17,262 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
      15:18:17,264 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
      15:18:17,264 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern /logs/jobs/%d{yyyy-MM-dd_HH:mm:ss}.CjA2HVpgTq.log for the active file
      15:18:17,265 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd_HH:mm:ss' from file name pattern '/logs/jobs/%d{yyyy-MM-dd_HH:mm:ss}.CjA2HVpgTq.log'.
      15:18:17,265 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over every second.
      15:18:17,265 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Thu Oct 25 15:18:17 PDT 2018
      15:18:17,265 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Cleaning on start up
      15:18:17,265 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - first clean up after appender initialization
      15:18:17,265 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - periodsElapsed = 336
      15:18:17,277 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE-CjA2HVpgTq] - Active log file name: /logs/jobs/CjA2HVpgTq.log
      15:18:17,277 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE-CjA2HVpgTq] - File property is set to [/logs/jobs/CjA2HVpgTq.log]
      15:18:17.261 INFO  c.l.t.logging.testing.LogbackTest - Hello, I am MDC
      15:18:19.277 INFO  c.l.t.logging.testing.LogbackTest - Hello, I am not MDC

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated: