In log4j 1.x we can directly use setFile method of FileAppender class
In log4j 2.x There is no setFile method to FileAppender class....
I want to statically initialize file name for an appender in log4j2.properties and then dynamically change the file name of that appender at run time.
How to acheive this?
You should add a new FileAppender and stop the old one.
Take a look at "Programmatically Modifying the Current Configuration after Initialization" in the log4j documentation
Here a my log4j2 file. A new log file will be created when the log file meet the policies. And the file name are specified in the appender.rolling.filePattern
, The system will create a new file everyday because of the %d{yyyy-MM-dd}
and the appender.rolling.policies.time.interval=1
. You can add more on %d{yyyy-MM-dd-HH}
and the log file will be generated every hour.
name=PropertiesConfig
property.filename= ./logs
appenders= rolling
appender.rolling.type=RollingFile
appender.rolling.name=RollingFile
appender.rolling.fileName=${filename}/here-are-my-log-propertieslogs.log
appender.rolling.layout.type=PatternLayout
appender.rolling.filePattern=${filename}/feedback-propertieslogs-%d{yyyy-MM-dd}-%i.log
appender.rolling.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}:%L - %msg%n
# Rotate log file each day and keep 30 days worth
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.policies.size.type=SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=5MB
appender.rolling.strategy.type=DefaultRolloverStrategy
appender.rolling.strategy.max=31
appender.rolling.strategy.delete.type=Delete
appender.rolling.strategy.delete.basePath=${filename}
appender.rolling.strategy.delete.maxDepth=1
appender.rolling.strategy.delete.ifLastModified.type=IfLastModified
appender.rolling.strategy.delete.ifLastModified.age=30d
appender.rolling.strategy.delete.ifAccumulatedFileCount.type=IfAccumulatedFileCount
appender.rolling.strategy.delete.ifAccumulatedFileCount.exceeds=30
appender.rolling.strategy.delete.ifAccumulatedFileSize.type=IfAccumulatedFileSize
appender.rolling.strategy.delete.ifAccumulatedFileSize.exceeds=100MB
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.