Q:

GROOVY + Slf4j + rotation and backup log files

the following groovy script generate the C:\tmp\groovy.log log file by the Slf4j
groovy script works fine and log increased each time we use log.info/log.debug ... etc
but the problem is when the log file became with huge capacity and each day the log become more larger
I wonder how to add to my script the capability of the rotation mechanism
for example what I need is when the log file become 100K , then log will backup like zip file ( it will decrease the capacity for example to 10K )
is it possible ?
if yes please advice how to change the groovy script in order to give the backup capability
@Grab('org.slf4j:slf4j-api:1.6.1')
@Grab('ch.qos.logback:logback-classic:0.9.28')

import org.slf4j.*
import groovy.util.logging.Slf4j
import ch.qos.logback.core.*
import ch.qos.logback.classic.encoder.*

// Use annotation to inject log field into the class.
@Slf4j
class Family {
    static {
        new FileAppender().with {
            name = 'file appender'
            file = 'C:\\tmp\\groovy.log'
            context = LoggerFactory.getILoggerFactory()
            encoder = new PatternLayoutEncoder().with {
                context = LoggerFactory.getILoggerFactory()
                pattern = "%date{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n"
                start()
                it
            }
            start()
            log.addAppender(it)
        }
    }

    def father() {
        log.debug 'car engine is hot'
        log.error 'my car is stuck'
    }

    def mother() {
        log.debug 'dont have a water in the kitchen'
        log.error 'Cant make a cake'
    }
}

def helloWorld = new Family()
helloWorld.father()
helloWorld.mother()

A:

You can try with RollingFileAppender instead of FileAppender and set the RollingPolicy you need. i.e you can try with:
...
@Slf4j
class Family {
    static {
        new RollingFileAppender().with {
            name = 'file appender'
            file = 'C:\\tmp\\groovy.log'
            // the policy to roll files
            rollingPolicy = new TimeBasedRollingPolicy().with{
                context = LoggerFactory.getILoggerFactory()
                // file name pattern for the rolled files
                fileNamePattern = 'C:\\tmp\\groovy.%date{yyyy-MM-dd}.%i.log'
                // the maximum number of files to be keeped.
                maxHistory = 10
                timeBasedFileNamingAndTriggeringPolicy = new SizeAndTimeBasedFNATP().with{
                    context = LoggerFactory.getILoggerFactory()
                    // the max size of each rolled file
                    maxFileSize = '3MB'
                }
            }
            context = LoggerFactory.getILoggerFactory()
            encoder = new PatternLayoutEncoder().with {
                context = LoggerFactory.getILoggerFactory()
                pattern = "%date{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n"
                start()
                it
            }
            start()
            log.addAppender(it)
        }
    }

...

Hope this helps,

