Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keeping log files more than week with a limit size

Tags:

logback

Im using logback to write to log files:

    <filter class="mypackage.logging.MarsLogFilter">
        <loggerName>mypackage.monitor</loggerName>  
        <level>DEBUG</level>
        <match>ACCEPT</match>
        <mismatch>DENY</mismatch>           
    </filter>

    <file>logs/taskMonitor.log</file>

    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <FileNamePattern>logs/archive/taskMonitor.%i.log.zip</FileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>12</maxIndex>
    </rollingPolicy>

    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
          <maxFileSize>300MB</maxFileSize>
    </triggeringPolicy>

    <encoder>
        <pattern>%d [%-5level] [%C:%L] - %m%n</pattern>
    </encoder>

</appender>

I want to keep logs for a week, the problem is that the logback with FixedWindowRollingPolicy keep max 12 files back

how could i keep more than 12 files with size 300MB

like image 254
Yosefarr Avatar asked Jan 20 '26 01:01

Yosefarr


1 Answers

That is the problem, you need to choose between time-based and size-based.

If your choice is time based: you have no control over the size. This is somehow logical, since a time based solution would be useless if you limit the size. You could loose a part of your log.

If your choice is size based: then you realy want to control your disk size. If something bad happens, then your disk size is safe, but your log-file can be very limited...

My personal solution is to create 2 appenders:

  • Time based: Keep info for some time, but log this in a higher log-level: Example: ERROR
  • Size based: Keep more detailed logging, but limit the size to protect your disk, when something 'bad' happens.

Time Based: roll every day, keep log 30 days

<appender name="DAILY" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/daily.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_HOME}/daily-%d{yyyy-MM-dd}.log.zip</fileNamePattern>
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>%date - %msg%n</pattern>
    </encoder>
</appender>

Size Based: protect the disk from to much log

<appender name="WATCHDOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/watchdog.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>${LOG_HOME}/watchdog-%i.log.zip</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>10</maxIndex>
    </rollingPolicy>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>100MB</maxFileSize>
    </triggeringPolicy>
    <encoder>
        <pattern>%date %-5level [%thread] - [%logger] - %msg%n</pattern>
    </encoder>
</appender>

You then can add 2 root loggers (something not really known to the public, but this is possible):

<root level="ERROR">
    <appender-ref ref="DAILY"/>
</root>
<root level="INFO">
    <appender-ref ref="WATCHDOG"/>
</root>
like image 176
Dimitri Dewaele Avatar answered Jan 23 '26 19:01

Dimitri Dewaele



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!