How to roll the log file on startup in logback

前端 未结 11 1684
梦谈多话
梦谈多话 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:30

    It works for me, using the following class as timeBasedFileNamingAndTriggeringPolicy :

    import java.io.File;
    import java.util.concurrent.atomic.AtomicBoolean;
    
    import ch.qos.logback.core.joran.spi.NoAutoStart;
    import ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP;
    
    @NoAutoStart
    public class Trigger extends SizeAndTimeBasedFNATP
    {
        private final AtomicBoolean trigger = new AtomicBoolean();
    
        public boolean isTriggeringEvent(final File activeFile, final E event) {
            if (trigger.compareAndSet(false, true) && activeFile.length() > 0) {
                String maxFileSize = getMaxFileSize();
                setMaxFileSize("1");
                super.isTriggeringEvent(activeFile, event);
                setMaxFileSize(maxFileSize);
                return true;
            }
            return super.isTriggeringEvent(activeFile, event);
        }
    }
    

提交回复
热议问题