How to roll the log file on startup in logback

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

    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.

    @NoAutoStart
    public class StartupSizeTimeBasedTriggeringPolicy extends     SizeAndTimeBasedFNATP {
    
        private boolean policyStarted;
    
        @Override
        public boolean isTriggeringEvent(File activeFile, E event) {
            if (!policyStarted) {
                policyStarted = true;
                if (activeFile.exists() && activeFile.length() > 0) {
                    nextCheck = 0L;
                    return true;
                }
            }
            return super.isTriggeringEvent(activeFile, event);
        }
    }
    

    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.

提交回复
热议问题