0

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?

phkr
  • 148
  • 3
  • 13
  • Possible duplicate of [Add file logging dynamically at runtime with Log4j2 2.8.1](https://stackoverflow.com/questions/43569394/add-file-logging-dynamically-at-runtime-with-log4j2-2-8-1) – D.B. Oct 07 '18 at 18:33

2 Answers2

-1

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
K.tin
  • 425
  • 2
  • 7
  • I meant ....How to change the log file name at runtime.........Like if some event occurs....i want to log to a different file after that event...........Not after specific intervals of time – phkr Oct 05 '18 at 13:20
-2

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

dyVeloper
  • 350
  • 2
  • 5
  • This would mean that the code depends on aspects of log4j2 that are not part of the public API. This is not desirable as it would mean any changes to the implementation could have a large impact on the client code. – D.B. Oct 07 '18 at 18:39