我是Elasticsearch用户,我必须使用log4j2.properties文件。 不幸的是我无法按照我的意愿删除日志。
我想要每天轮换一些日志文件(对应一个模式)。 我还希望删除旋转的日志文件,如果它符合我的两个条件之一:
我尝试使用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时,收到错误消息:
主要错误IfAccumulatedFileSize包含无效元素或 属性“年龄”
我真的很感激这方面的任何帮助。谢谢你的关注!
答案 0 :(得分:3)
尝试按以下方式更改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配置。但是,通过参考这些示例,您还可以思考和猜测属性配置。
答案 1 :(得分:0)
@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中找到示例。 让我知道是否有帮助!
答案 2 :(得分:0)
@gino thx 是你很好的例子
以下是对 Elasticsearch 7.9.1 中原始 log4j2.properties
和部分 Server JSON
的修改
它将删除扩展名为 .gz
且修改日期超过 14 天或累积大小大于 1GB 的文件(请从 Delete
行开始替换配置)
如果您不更改间隔,则在每天轮换 2 次 elasticsearch 日志后,日志文件计数的变化将可见。
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