Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can not configure log4j2 properties file with multiple conditions

I am an Elasticsearch user and I have to use a log4j2.properties file. Unfortunately i can't get it to delete logs as I would like.

I want some log files (corresponding to a pattern) rotated every day. I also want the rotated log files to be deleted if it matches one of my 2 conditions:

  • if they are more than 3 months old
  • if the total size of these files exceeds 200 mega.

I try to use the PathCondition "ifany", described in this piece of log4j2 documentation: https://logging.apache.org/log4j/2.x/manual/appenders.html

Here is my log4j2.properties file:

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

Currently, when I restart log4j2, I get the error message:

main ERROR IfAccumulatedFileSize contains an invalid element or attribute "age"

I would really appreciate any help on this subject. Thanks for your attention!

like image 238
ESCURE Thomas Avatar asked Jan 02 '23 21:01

ESCURE Thomas


1 Answers

Trying Changing your configuration for DeleteAction as per below -

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

You can also refer to log4j2 documentation for configuring multiple conditions. Log4j2 documentation describes XML configuration. However, by referring to these examples, you can think and guess properties configuration also.

like image 180
Vikas Sachdeva Avatar answered Jan 31 '23 05:01

Vikas Sachdeva