简体   繁体   English

如何在 log4j 版本 2 中动态设置 FileAppender 的文件名?

[英]How to set file name of FileAppender dynamically in log4j version 2?

In log4j 1.x we can directly use setFile method of FileAppender class在 log4j 1.x 中我们可以直接使用 FileAppender 类的 setFile 方法

In log4j 2.x There is no setFile method to FileAppender class....在 log4j 2.x 中 FileAppender 类没有 setFile 方法....

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.我想在 log4j2.properties 中静态初始化 appender 的文件名,然后在运行时动态更改该 appender 的文件名。

How to acheive this?如何实现这一目标?

You should add a new FileAppender and stop the old one.您应该添加一个新的 FileAppender 并停止旧的。

Take a look at "Programmatically Modifying the Current Configuration after Initialization" in the log4j documentation看一下log4j文档中的“Programmatically Modifying the Current Configuration after Initialization”

Here a my log4j2 file.这是我的 log4j2 文件。 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 .并且文件名在appender.rolling.filePattern中指定,由于%d{yyyy-MM-dd}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.您可以在%d{yyyy-MM-dd-HH}上添加更多内容,并且每小时都会生成日志文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM