OldCurmudgeon
OldCurmudgeon

Reputation: 65859

Logback rolling file appender - can I zip many logs into one file?

My current logback config looks like this:

<appender name="rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <file>${log.dir}/${log.package}.log</file>
  <encoder>
    <Pattern>${log.pattern}</Pattern>
  </encoder>
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <fileNamePattern>${log.dir}/${log.package}.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
    <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
      <!-- or whenever the file size reaches 1MB. -->
      <maxFileSize>1MB</maxFileSize>
    </timeBasedFileNamingAndTriggeringPolicy>
    <!-- Keep no more than 3 months data. -->
    <maxHistory>90</maxHistory>
    <cleanHistoryOnStart>true</cleanHistoryOnStart>
  </rollingPolicy>
</appender>

This works fine but irritatingly creates multiple zip file ...1.zip ...2.zip etc.

Is there any way I can specify the zip file name as ${log.dir}/${log.package}.%d{yyyy-MM-dd}.log.zip but the name of the files in the zip file as ${log.dir}/${log.package}.%i.log? I.e. make one zip file per day but each time the file reaches 1mb I zip it as ....1.log, ...2.log etc.

Upvotes: 2

Views: 6087

Answers (1)

Karthick Radhakrishnan
Karthick Radhakrishnan

Reputation: 1161

Yes you can add multiple files into a zip file using rollingfileappender with FixedWindowRollingPolicy.

<appender name="ErrorLogs"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <target>System.err</target>     
        <file>${log_location}{Error_filename}</file>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${log_location}{ERR_Zip}</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>3</maxIndex>
        </rollingPolicy>
        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>${maxFileSize}</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>${Default_Pattern}</pattern>
        </encoder>
    </appender>

since you cannot use both timebased rolling and sizebased rolling my suggestion would be to set high value for maxfilesize so that neither of these constraint can be minimalised

Upvotes: 1

Related Questions