I would like to configure logback to do the following.
- Log to a file
- Roll the file when it reaches 50MB
- Only keep 7 days worth of logs
- On startup always generate a new file (do a roll)
I have it all working except for the last item, startup roll. Does anyone know how to achieve that? Here's the config...
<appender name="File" class="ch.qos.logback.core.rolling.RollingFileAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg \(%file:%line\)%n</Pattern>
</layout>
<File>server.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>server.%d{yyyy-MM-dd}.log</FileNamePattern>
<!-- keep 7 days' worth of history -->
<MaxHistory>7</MaxHistory>
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>50MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
I finally figure it out. I can roll by size, time and start up. Here is solution:
1st create you own class
2nd configure logback
Ceki's solution doesn't appear to work for me, but seems to be part way there at least.
It blows up because it can't see the rolling policy when starting the
TimeBasedFileNamingAndTriggeringPolicyBase
. With some hackery I got it to do some logging, and with some more I got it to observe the trigger, but then it broke again because it couldn't resolve one of the filename properties... The package is a logback one so I could get to some of the internals, to replicate some of the logic inSizeAndTimeBasedFNATP#isTriggeringEvent
and callcomputeCurrentPeriodsHighestCounterValue
. I think something along those lines might work, just haven't found the magic combination yet. I really hope I'm doing something silly, because otherwise I think it will mean either opening up some of the details for subclassing, or putting this straight into logback as another rolling/triggering policy.logback.xml: tried various orderings of
triggeringPolicy
,TimeBasedFileNamingAndTriggeringPolicy
inside and outside therollingPolicy
.The trigger policy:
The exception:
For a solution using already existing components the logback suggests the uniquely named files: http://logback.qos.ch/manual/appenders.html#uniquelyNamed
UPDATED for logback-1.2.1
This solution really works, thanks a lot. However, there is one annoying glitch: when you run the program first time, the log is rolled right after it is created, when it is empty or almost empty. So I suggest a fix: check whether the log file exists and is not empty at the time the method is called. Also, one more cosmetic fix: rename the "started" variable, because it is hiding the inherited member with the same name.
Also, I believe it works properly with logback version 1.1.4-SNAPSHOT (I got the source and compiled it myself), but it does not fully work with 1.1.3 release. With 1.1.3, it names the files properly with the specified time zone, but rollover still happens at default time zone midnight.
Create your own subclass of
ch.qos.logback.core.rolling.TimeBasedRollingPolicy
and override itsstart