TimeBasedTriggeringPolicy log4j2 not rolling over when new day happens

允我心安 提交于 2021-02-08 11:37:49

问题


I have configured Log4j2 with the following configuration, but TimeBasedTriggeringPolicy is not working, I am getting new day logs in older day's logs, can you please help. It is rolling over properly with size and it is also creating any number of files since I have specified "nomax" attribute for DefaultRollOverStrategy, only TimeBasedTriggeringPolicy is not working. It writes logs of a day in the previous day's log file. It does create the new log file with the latest date, but some logs have been logged in to the previous day's log. May be getting problem in naming the files properly I do not know.

  {
  "Configuration": {
    "Properties": {
      "Property": [
        {
          "name": "application",
          "value": "myapp"
        }
      ]
    },
    "Appenders": {
      "Console": {
        "name": "Console-Appender",
        "target": "SYSTEM_OUT",
        "PatternLayout": {
          "pattern": "%d{yyyy-MM-dd HH:mm:ss,SSS} ${application} %-5level %marker %t %c{5}  %msg%n"
        },
         "ThresholdFilter": { "level": "error" }
      },
      "RollingFile": [
        {
          "name": "File-Appender",
          "fileName":"${sys:log.path}/${application}.log",
          "filePattern":"${sys:log.path}/${application}-%d{yyyy-MM-dd}-%i.log",
          "PatternLayout": {
            "pattern": "%d{yyyy-MM-dd HH:mm:ss,SSS} ${application} %-5level %marker %t %c{5}  %msg%n"
          },
          "Policies": {
            "TimeBasedTriggeringPolicy": {"interval":"1", "modulate":"true" },
             "SizeBasedTriggeringPolicy": { "size": "5 KB" } 
            },
            "DefaultRolloverStrategy": {"fileIndex":"nomax"}   
        }
      ]
    },
    "loggers": {
      "logger":{
        "name": "com.mycompany",
        "level": "${sys:log.level}",
        "AppenderRef": { "ref": "File-Appender"}
      },
      "root": {
        "level": "error",
        "AppenderRef": { "ref": "Console-Appender" }
      }
    }
  }
}

回答1:


How about the CronTriggeringPolicy? This will trigger a rollover for you every day at 12AM:

{"CronTriggeringPolicy": {"schedule": "0 0 0,12 * * ?"}

That way, it won't rely on the file pattern and should definitely roll your logs over per your cron interval. I also don't know how you determined that the previous days logs were included in the rollover, but make sure your system time is in sync.




回答2:


Adding :

"OnStartupTriggeringPolicy": {"minSize":"0"}

to the policies solved the problem.

From log4j2 documentation: OnStartup Triggering Policy The OnStartupTriggeringPolicy policy causes a rollover if the log file is older than the current JVM's start time and the minimum file size is met or exceeded. OnStartupTriggeringPolicy Parameters: minSize: long: The minimum size the file must have to roll over. A size of zero will cause a roll over no matter what the file size is. The default value is 1, which will prevent rolling over an empty file



来源:https://stackoverflow.com/questions/48636801/timebasedtriggeringpolicy-log4j2-not-rolling-over-when-new-day-happens

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!