How to roll the log file on startup in logback

前端 未结 11 1749
梦谈多话
梦谈多话 2020-12-02 12:50

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
  • <
11条回答
  •  悲哀的现实
    2020-12-02 13:50

    I got the following to work (combining ideas from previous answers). Note I was dealing with size-based files, not time-based, but I am guessing the same solution works.

    public class StartupSizeBasedTriggeringPolicy extends ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy {
    
    private final AtomicReference isFirstTime = new AtomicReference(true);
    
    @Override
    public boolean isTriggeringEvent(final File activeFile, final E event) {
    
        //this method appears to have side-effects so always call
        boolean result = super.isTriggeringEvent(activeFile, event);
    
        return isFirstTime.compareAndSet(true, false) || result;
    }
    

    }

提交回复
热议问题