新的一天发生时,TimeBasedTriggeringPolicy log4j2不会滚动 [英] TimeBasedTriggeringPolicy log4j2 not rolling over when new day happens

查看:83
本文介绍了新的一天发生时,TimeBasedTriggeringPolicy log4j2不会滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用以下配置配置了Log4j2,但TimeBasedTriggeringPolicy无法正常工作,我在较旧的日志中获取了新的日志,请您帮忙.由于已为DefaultRollOverStrategy指定了"nomax"属性,因此它会随着大小正确滚动,并且还会创建任意数量的文件,只有TimeBasedTriggeringPolicy无法正常工作.它将前一天的日志写入前一天的日志文件中.它会使用最新日期创建新的日志文件,但是某些日志已登录到前一天的日志中.我不知道正确命名文件时可能会遇到问题.

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" }
      }
    }
  }
}

推荐答案

添加中:

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

通过政策解决了问题.

来自 log4j2文档:

如果日志文件早于当前JVM的启动时间并且已达到或超过最小文件大小,则OnStartupTriggeringPolicy策略会导致过渡.

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参数:

OnStartupTriggeringPolicy Parameters:

minSize:long:文件必须翻转的最小大小.大小为零将导致翻转,无论文件大小如何.默认值为1,这将防止将空文件翻转

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

这篇关于新的一天发生时,TimeBasedTriggeringPolicy log4j2不会滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆