如何使 log4j2 每次日志调用记录到一个文件中?

huangapple go评论107阅读模式
英文:

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 )

huangapple
  • 本文由 发表于 2020年10月25日 23:10:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64525270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定