Search code examples
apache-karafpax

org.ops4j.pax.logging.cfg with TimeBasedRollingPolicy


I need to write Apache Karaf logging using in built pax logging to have it in TimeBasedRollingPolicy with daily rolling.

As the code is hidden, I can only overwrite the properties to utilize TimeBasedRollingPolicy.

Q1) Is TimeBasedRollingPolicy supported in Apache Karaf 4.2.9? Q2) If yes, can anyone share the sample cfg file with TimeBasedRollingPolicy and associated properties to be used?

I don't want SizeBasedTriggeringPolicy, just TimeBasedRollingPolicy. And I have to handle this through code.

org.ops4j.pax.logging.cfg appears like this:

`# Rolling file appender
log4j2.appender.rolling.type = RollingRandomAccessFile
log4j2.appender.rolling.name = RollingFile
log4j2.appender.rolling.fileName = [path]/karaf.log
log4j2.appender.rolling.filePattern = [path]/app-name-%d{yyyy-MM-dd-HH-mm}-%i.log
# uncomment to not force a disk flush
#log4j2.appender.rolling.immediateFlush = false
log4j2.appender.rolling.append = true
log4j2.appender.rolling.layout.type = PatternLayout
log4j2.appender.rolling.layout.pattern = %d %p %C{1.} [%t] %m%n
log4j2.appender.rolling.policies.type = Policies
log4j2.appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.rolling.policies.size.size = 16MB

# Audit file appender
log4j2.appender.audit.type = RollingRandomAccessFile
log4j2.appender.audit.name = AuditRollingFile
log4j2.appender.audit.fileName = [path]/security.log
log4j2.appender.audit.filePattern = [path]/security.log.%i
log4j2.appender.audit.append = true
log4j2.appender.audit.layout.type = PatternLayout
log4j2.appender.audit.layout.pattern = %m%n
log4j2.appender.audit.policies.type = Policies
log4j2.appender.audit.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.audit.policies.size.size = 8MB

# OSGi appender
log4j2.appender.osgi.type = PaxOsgi
log4j2.appender.osgi.name = PaxOsgi
log4j2.appender.osgi.filter = *
log4j2.appender.rolling.policies.time.type = TimeBasedRollingPolicy
log4j2.appender.rolling.policies.time.interval = 1
log4j2.appender.rolling.policies.time.modulate = TRUE
log4j.appender.out.file = ${mkv.logsdir}/karaf.log`

Solution

  • First - check Log4j2 RolloverStrategies - there's no such thing as TimeBasedRollingPolicy (the name is available in Logback).

    There's however TimeBasedTriggeringPolicy and here's working configuration that rolls over every 15 second:

    log4j2.appender.timerolling.type = RollingRandomAccessFile
    log4j2.appender.timerolling.name = TimeRollingFile
    log4j2.appender.timerolling.fileName = ${karaf.log}/time.log
    log4j2.appender.timerolling.filePattern = ${karaf.log}/time.log.%d{yyyy-MM-dd-HH-mm-ss}
    log4j2.appender.timerolling.append = true
    log4j2.appender.timerolling.layout.type = PatternLayout
    log4j2.appender.timerolling.layout.pattern = ${log4j2.pattern}
    log4j2.appender.timerolling.policies.type = Policies
    log4j2.appender.timerolling.policies.time.type = TimeBasedTriggeringPolicy
    log4j2.appender.timerolling.policies.time.interval = 15
    

    The interval=15 may be confusing, but (after reading the source) it means "roll over every 15 of the least unit specified in file pattern". So if the pattern was ${karaf.log}/time.log.%d{yyyy-MM} you'd have trigger every 15 months.

    With the above setting and waiting a bit I have these files:

    time.log
    time.log.2023-02-24-09-43-48
    time.log.2023-02-24-09-44-18
    time.log.2023-02-24-09-44-34