使用 Log4j 创建每日日志?

发布于 2024-08-11 05:02:34 字数 902 浏览 2 评论 0原文

设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不必你懂 2024-08-18 05:02:34

我根据这篇博客文章制定了此配置,但它的行为方式与他描述的不同。日志正在创建为 MyApp,不带扩展名。

这就是您告诉 Log4J 对此行执行的操作:

<param name="file" value="MyApp"/>

Log4J 使用此选项的值作为日志文件名,并且仅在发生翻转时应用该模式,如 记录

例如,如果设置了文件选项
/foo/bar.log 和 DatePattern
设置为“.”yyyy-MM-dd,于 2001 年 2 月 16 日
午夜,日志文件
/foo/bar.log 将被复制到
/foo/bar.log.2001-02-16 和日志记录
2001-02-17 将继续
/foo/bar.log 直到它滚过
第二天。

所以对我来说一切看起来都很正常。

要获得所需的行为,您可以编写自己的 Appender。看起来 James Stauffer 做了一些非常类似的事情(请参阅 这个答案),但他扩展了FileAppender。不过,将他的工作改编为 DailyRollingFileAppender 子类应该很容易。

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.

This is what you told Log4J to do with this line:

<param name="file" value="MyApp"/>

Log4J uses the value of this option as log file name and the pattern is only applied when the roll over occurs, as documented:

For example, if the File option is set
to /foo/bar.log and the DatePattern
set to '.'yyyy-MM-dd, on 2001-02-16 at
midnight, the logging file
/foo/bar.log will be copied to
/foo/bar.log.2001-02-16 and logging
for 2001-02-17 will continue in
/foo/bar.log until it rolls over the
next day.

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 extends FileAppender. It should be easy to adapt his work for a DailyRollingFileAppender subclass though.

ぃ弥猫深巷。 2024-08-18 05:02:34

当前日志文件名为 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.

君勿笑 2024-08-18 05:02:34

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).

忘年祭陌 2024-08-18 05:02:34

回应您对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文