我无法配置具有多个条件的 log4j2 属性文件



我是Elasticsearch用户,我必须使用log4j2.properties文件。不幸的是,我无法让它像我想要的那样删除日志。

我希望每天轮换一些日志文件(对应于模式(。我还希望删除轮换的日志文件,如果它符合我的 2 个条件之一:

  • 如果他们超过3个月大
  • 如果这些文件的总大小超过 200 兆。

我尝试使用PathCondition"ifany",在log4j2文档中描述:https://logging.apache.org/log4j/2.x/manual/appenders.html

这是我的log4j2.properties文件:

status = error
# log action execution errors for easier debugging
logger.action.name = org.elasticsearch.action
logger.action.level = debug
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%m%n
appender.rolling.type = RollingFile
appender.rolling.name = rolling
appender.rolling.fileName = ${sys:es.logs}.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%.-10000m%n
appender.rolling.filePattern = ${sys:es.logs}-%d{yyyy-MM-dd}.log
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath = /var/log/elasticsearch
appender.rolling.strategy.action.condition.type = IfFileName
appender.rolling.strategy.action.condition.glob = mylog-*.log
appender.rolling.strategy.action.PathConditions.type = IfAny
appender.rolling.strategy.action.PathConditions.nestedConditions.type = IfLastModified
appender.rolling.strategy.action.PathConditions.nestedConditions.age = 90D
appender.rolling.strategy.action.PathConditions.nestedConditions.type = IfAccumulatedFileSize
appender.rolling.strategy.action.PathConditions.nestedConditions.exceeds = 200M

目前,当我重新启动log4j2时,我收到错误消息:

主要错误 如果累积文件大小包含无效元素或 属性"年龄">

我非常感谢有关此主题的任何帮助。感谢您的关注!

尝试按如下方式更改DeleteAction的配置 -

appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath = /var/log/elasticsearch
appender.rolling.strategy.action.maxDepth = 1
appender.rolling.strategy.action.condition.type = IfFileName
appender.rolling.strategy.action.condition.glob = mylog-*.log
appender.rolling.strategy.action.ifAny.type = IfAny
appender.rolling.strategy.action.ifAny.ifLastModified.type = IfLastModified
appender.rolling.strategy.action.ifAny.ifLastModified.age = 90d
appender.rolling.strategy.action.ifAny.ifAccumulatedFileSize.type = IfAccumulatedFileSize
appender.rolling.strategy.action.ifAny.ifAccumulatedFileSize.exceeds = 200MB

您还可以参考log4j2文档来配置多个条件。 Log4j2文档介绍了 XML 配置。但是,通过参考这些示例,您也可以思考和猜测属性配置。

@Vikas答案对我不起作用。我必须在每个ifAny.*之前添加.condition。像这样:

appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.fileIndex = nomax
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
appender.rolling.strategy.action.condition.type = IfFileName
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-*.json.gz
appender.rolling.strategy.action.condition.ifAny.type = IfAny
appender.rolling.strategy.action.condition.ifAny.ifLastModified.type = IfLastModified
appender.rolling.strategy.action.condition.ifAny.ifLastModified.age = 30D
appender.rolling.strategy.action.condition.ifAny.ifAccumulatedFileSize.type = IfAccumulatedFileSize
appender.rolling.strategy.action.condition.ifAny.ifAccumulatedFileSize.exceeds = 7GB

请在此处找到示例 https://discuss.elastic.co/t/log4j2-properties-an-example-of-a-multiple-conditions-configuration-for-an-elasticsearch-7-cluster-7-6-1/249939。让我知道它是否有帮助!

@gino谢谢

你的很好的例子

以下是对 Elasticsearch 7.9.1
中原始log4j2.properties和第 Server JSON 节的修改它将删除扩展名.gz修改日期超过 14 天或累积大小大于 1GB 的文件(请从Delete行开始替换配置(

如果您没有更改间隔,则在 Elasticsearch 日志每天轮换 2 次后,日志文件计数的变化将可见。

appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
appender.rolling.strategy.action.condition.type = IfFileName
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-*.gz
appender.rolling.strategy.action.condition.ifAny.type = IfAny
appender.rolling.strategy.action.condition.ifAny.ifLastModified.type = IfLastModified
appender.rolling.strategy.action.condition.ifAny.ifLastModified.age = 14D
appender.rolling.strategy.action.condition.ifAny.ifAccumulatedFileSize.type = IfAccumulatedFileSize
appender.rolling.strategy.action.condition.ifAny.ifAccumulatedFileSize.exceeds = 1GB

最新更新