Log4j2 customize file path with rollingFileAppender (Java)

You typically do not touch the implementation code of log4j2 classes. What you’re trying to do can be done through configuration.

https://logging.apache.org/log4j/2.x/manual/migration.html https://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender

You can do what you’re asking like this

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${BASE_DIR}/application.log
appender.rolling.filePattern = ${BASE_DIR}/application.%d{dd-MMM}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
logger.rolling.name = rollingFile
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

where BASE_DIR is an environment variable

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top