1

I am using logback for rollover of log files. I am able to fix the size of each file, and max number of files each day. But i have one more requirement to complete, i.e. the total number of size should not exceed a maximum number. To elaborate, say i have per day files limit is 10, now i want to set max file limit 50. So any day after 5th day(not necessarily exactly after 5th day. It may happen that one certain day only 2 files are generated. ) it will reach maximum numbers of file.

this is what my current code looks like. Can anyone please edit this to achieve the result i am expecting

<appender name="MAINLOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>./logs/usageHub.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxHistory>90</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 100MB -->
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <charset>utf-8</charset>
            <Pattern>[%p] %d{yyyy-MMM-dd HH:mm:ss,SSS} %c - %m%n</Pattern>
        </encoder>
    </appender>
koushik
  • 55
  • 7

1 Answers1

2

We have configured FixedWindowRollingPolicy as below which is working and keeps only 5 files and of max size limit as 50MB.

<appender name="rollingTransportLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${log.folder}/cd_transport.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${log.folder}/cd_transport.%i.log</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>5</maxIndex>
            </rollingPolicy>
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <maxFileSize>50MB</maxFileSize>
    </triggeringPolicy>
         <encoder>
            <pattern>${log.pattern}</pattern>
        </encoder>
<prudent>false</prudent>
    </appender>

More details about Logback appenders can be found here. Hope this helps.

Raghuveer
  • 2,859
  • 7
  • 34
  • 66