0

I want to hold up to 20 log files, where I rollback when a file reaches a certain size. The catch is - when I archive a file I want to to have the current time in its' name.

Currently I use:

    <timestamp key="bySecond" datePattern="ddMMyyyy'-'HHmmss" timeReference="contextBirth"/>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${application.home}/logs/log.txt</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${application.home}/logs/${bySecond}.%i.log</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>30</maxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>2KB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%date [%level] [%thread] from %logger  - %message%n%xException</pattern>
        </encoder>
    </appender>

But the problem here is the bySecond timestamp is evaluated only once at service start-up.

What I want is the current time for each rollover. How can I achieve this?

BTW, If i could get rid of the necessity to use the %i in the file name, it would be great!

Elliko
  • 71
  • 1
  • 10

1 Answers1

2

I had this problem and the final solution I tried is extending the Rolling Policy Class. For example we want to change the name of the file which it is set by a method named setFileNamePattern in FixedWindowRollingPolicy. So the only thing you have to do is extending FixedWindowRollingPolicy by a custom class and overriding this method and calling the super one and append the current time to it. Finally, use this custom class name in class property of rollingPolicy in logback.xml.

import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
import java.time.LocalDate;

public class MyRollingPolicy extends FixedWindowRollingPolicy {
    @Override
    public void setFileNamePattern(String fnp) {
        super.setFileNamePattern(fnp + LocalDate.now());
    }
}