使用 Log4j 创建每日日志?
设置 Log4j 以使用以下模式需要哪些配置值?
MyApp-Mon.log
MyApp-Tue.log
MyApp-Wed.log
等等
每个文件都包含天数日志。
这听起来很容易使用 Log4j 的 DailyRollingFileAppender 来完成,但我遇到了麻烦。
这是我当前的配置;
<appender name="daily-file" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="info"/>
<param name="DatePattern" value="'-'EE'.log'"/>
<param name="file" value="MyApp"/>
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{EEE MMM dd hh:mm:ss zzz yyyy} %-5p %l - %m%n"/>
</layout>
</appender>
我将此配置基于此博客帖子,但它的行为并不像他描述的那样。日志被创建为 MyApp,没有扩展名。
有人可以帮我吗?
What configuration values are needed to setup Log4j to use the following pattern?
MyApp-Mon.log
MyApp-Tue.log
MyApp-Wed.log
Etc
With each file containing the days log.
This sounds easy enough to do with Log4j's DailyRollingFileAppender but I am having trouble.
Here is my current config;
<appender name="daily-file" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="info"/>
<param name="DatePattern" value="'-'EE'.log'"/>
<param name="file" value="MyApp"/>
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{EEE MMM dd hh:mm:ss zzz yyyy} %-5p %l - %m%n"/>
</layout>
</appender>
I based this config on this blog post, but it is not behaving in the way he describes. The log is being created as MyApp with no extension.
Can anyone help me out here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是您告诉 Log4J 对此行执行的操作:
Log4J 使用此选项的值作为日志文件名,并且仅在发生翻转时应用该模式,如 记录:
所以对我来说一切看起来都很正常。
要获得所需的行为,您可以编写自己的
Appender
。看起来 James Stauffer 做了一些非常类似的事情(请参阅 这个答案),但他扩展了FileAppender
。不过,将他的工作改编为DailyRollingFileAppender
子类应该很容易。This is what you told Log4J to do with this line:
Log4J uses the value of this option as log file name and the pattern is only applied when the roll over occurs, as documented:
So everything looks normal to me.
To obtain the desired behavior, you could write your own
Appender
. It looks like James Stauffer did something very similar (see this answer) but he extendsFileAppender
. It should be easy to adapt his work for aDailyRollingFileAppender
subclass though.当前日志文件名为 MyApp,那么明天它将被重命名为 Myapp-yesterday,依此类推,
但当前日志始终使用默认名称。
the current logfile is named MyApp, then tomorrow it will be renamed by Myapp-yesterday and so on
but the current log has always the default name.
查看 DailyRollingFileAppender 的 JavaDocs< /a>,他们列出的所有示例都有特定的数字日期模式,而不是日期名称模式。您是否尝试过使用他们的示例之一是否有效?如果是这样,而您的没有,那么我假设 DailyRollingFileAppender 不支持使用包含“E”(星期几)的日期模式。
Looking at the JavaDocs for the DailyRollingFileAppender, all the examples they set out have specific, numeric date patterns, not day-name patterns. Have you tried to see if using one of their examples works? If so, and yours doesn't, then I'd assume that the DailyRollingFileAppender doesn't support the use of a date pattern containing 'E' (the day of the week).
回应您对 Pascal 答案的评论:
ln -s MyApp.log MyApp-NOW.log
有效吗?您的旧日志查看器将通过符号链接显示 MyApp-NOW.log,并且 log4j 可以在午夜将 MyApp.log 滚动到 MyApp-TUE.log。In response to your comment to Pascal's answer:
Would a
ln -s MyApp.log MyApp-NOW.log
work? Your legacy log viewer would show MyApp-NOW.log via the symbolic link and log4j can roll-over MyApp.log to MyApp-TUE.log at midnight.