英文:
How to make log4j2 log one file per log call?
问题
有没有在log4j2中配置文件appender的[良好实践]方式,使其每次调用都生成一个带有时间戳的文件?
log.error("在文件1中");
log.error("在文件2中");
英文:
Is there a [good practice] way in log4j2 to configure a file appender so it produces one timestamped file per call?
log.error("in file 1");
log.error("in file 2");
答案1
得分: 0
那就是这样:
<RollingFile name="MyAppender"
filePattern="/mylogs/%d{yyyy-MM-dd-HH_mm_ss_SSS}.error.log"
append="false">
<PatternLayout pattern="%msg%n" />
<TimeBasedTriggeringPolicy />
<DirectWriteRolloverStrategy />
</RollingFile>
<DirectWriteRolloverStrategy />
允许 appender 直接使用 filePattern 属性。
<TimeBasedTriggeringPolicy />
允许 appender 每次模式不同时(在这种情况下是毫秒 _SSS
),写入不同的文件。
英文:
That would be it:
<RollingFile name="MyAppender"
filePattern="/mylogs/%d{yyyy-MM-dd-HH_mm_ss_SSS}.error.log"
append="false">
<PatternLayout pattern="%msg%n" />
<TimeBasedTriggeringPolicy />
<DirectWriteRolloverStrategy />
</RollingFile>
<DirectWriteRolloverStrategy />
is allowing the appender to directly use the filePattern attribute.
<TimeBasedTriggeringPolicy />
is allowing the appender to write to a different file each time the pattern is different ( in this case milliseconds _SSS
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论