代码之家  ›  专栏  ›  技术社区  ›  ESCURE Thomas

我无法在多个条件下配置log4j2属性文件

  •  4
  • ESCURE Thomas  · 技术社区  · 8 年前

    我是Elasticsearch用户,必须使用log4j2。属性文件。 不幸的是,我无法让它按照我的意愿删除日志。

    我想每天旋转一些日志文件(对应于一个模式)。 如果旋转的日志文件符合以下两个条件之一,我还希望将其删除:

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

    我尝试使用这段log4j2文档中描述的路径条件“ifany”: https://logging.apache.org/log4j/2.x/manual/appenders.html

    这是我的日志4J2。属性文件:

    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时,会收到错误消息:

    如果AccumulatedFilesize包含无效元素或 属性“年龄”

    如果能在这个问题上得到任何帮助,我将不胜感激。感谢您的关注!

    3 回复  |  直到 8 年前
        1
  •  9
  •   Vikas Sachdeva    8 年前

    正在尝试更改的配置 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 documentation 用于配置多个条件。 Log4j2 文档描述了XML配置。然而,通过参考这些示例,您也可以思考和猜测属性配置。

        2
  •  0
  •   gino    6 年前

    @维卡斯的回答对我不起作用。我不得不补充一点。每个ifAny前的条件。*。类似于:

    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 . 如果有帮助,请告诉我!

        3
  •  0
  •   tkrakowian    5 年前

    @gino thx为您提供了一个很好的示例

    以下是对原文的修改 log4j2.properties 和部分 Server JSON 在Elasticsearch 7.9.1中
    它将删除扩展名为的文件 .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