2

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.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213

1 Answers1

1

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

Karthick Radhakrishnan
  • 1,151
  • 3
  • 12
  • 32
  • Is there any way I can do both time based and fixed window? I.e. one zip per day AND trigger a fixed window on size as you have? – OldCurmudgeon Jun 09 '14 at 11:56
  • no direct way to do that, but what we can do is increase your max filesize, increase you maxindexsize to 25, assume your log file writes 25MB of data and your maxsize is 1 MB, whe it reaches 25 log files with each 1 MB then your log files will be zipped into one file. – Karthick Radhakrishnan Jun 09 '14 at 12:18
  • please refer this post answered by the logback author ceki http://stackoverflow.com/questions/7934829/logback-set-max-history-files-per-day – Karthick Radhakrishnan Jun 09 '14 at 12:20