2

Hi is there some way how set to roll log when application start or if is a new day and keep history for 30 days or 1 month (not 30 files) by a configuration file. I looked for it a several hours but fount nothing. Probably this way doesn't exist and need to be write.

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d [%thread] %-5level %logger{50} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/logs/server.log</file>
        <encoder>
            <pattern>%d [%thread] %-5level %logger{50} - %msg%n
            </pattern>
        </encoder>

        <!-- Join this time based rolling with-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/logs/server.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
            <!-- Mean 30 days not 30 files -->
            <maxHistory>30</maxHistory> 
        </rollingPolicy>

        <!-- New start baser Rolling-->
        ...
    </appender>

    <root level="INFO">
    <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

</configuration>
Perlos
  • 2,028
  • 6
  • 27
  • 37
  • When rolling is done daily, 30 files is equivalent to 30 days. Thus, I am not sure I understand you question. – Ceki May 20 '12 at 17:48
  • I need react to START and NEW DAY. So when i start several times on the day a will have several files more. Something like this: 30 days = 30 files + 10 restart = 10 files => 40 files on month. I want keep all these files unless there are older then 30 days (i take date when file was created) – Perlos May 28 '12 at 17:35
  • Looking at the configuration file above, I can affirm that when you restart you application you will *not* have a new log file. – Ceki May 29 '12 at 11:47
  • @Ceki: I know it, because ask how to do it – Perlos May 30 '12 at 12:03
  • If you know that when you restart your application there *won't* be a new log file, then your question does not makes sense. – Ceki May 30 '12 at 14:38
  • @Ceki: Therefore I'm asking if is there any combination of settings that would make it working. First of all I'm asking how to achieve a new division when application start or when start a new day – Perlos Jun 01 '12 at 10:22
  • Possible duplicate of [How to roll the log file on startup in logback](https://stackoverflow.com/questions/2492022/how-to-roll-the-log-file-on-startup-in-logback) – Draken Dec 11 '17 at 10:45

1 Answers1

3

This question is duplicated and answered by https://stackoverflow.com/a/30801658/844648

I finally figure it out. I can roll by size, time and start up. Here is solution:

1th create you own class

@NoAutoStart
public class StartupSizeTimeBasedTriggeringPolicy<E> extends SizeAndTimeBasedFNATP<E> {

    private boolean started = false;

    @Override
    public boolean isTriggeringEvent( File activeFile, E event ) {
        if ( !started ) {
            nextCheck = 0L;
            return started = true;
        }

        return super.isTriggeringEvent( activeFile, event );
    };
}

2th configure lockbax

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOGS_DIR}/${FILE_NAME}.log</file>
    <encoder>
        <pattern>%d [%thread] %-5level %logger{50} - %msg%n</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOGS_DIR}/${FILE_NAME}.%d{yyyy-MM-dd}_%d{HHmmss,aux}.%i.log.zip</fileNamePattern>
        <maxHistory>30</maxHistory>
        <TimeBasedFileNamingAndTriggeringPolicy class="my.StartupSizeTimeBasedTriggeringPolicy">
            <MaxFileSize>250MB</MaxFileSize> 
        </TimeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>
Community
  • 1
  • 1
Perlos
  • 2,028
  • 6
  • 27
  • 37