Log4j中包含日期的文件名 [英] Filename with date in Log4j

查看:860
本文介绍了Log4j中包含日期的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将当前日期附加到log4j日志文件中。所以它会是这样的:

I'm trying to append the current date to the log4j log file. So it would be something like this:

myApp-2011-01-07.log

myApp-2011-01-07.log

事情是我不想使用DailyRollingFileAppender。原因是每天都会运行另一个脚本来备份logs文件夹中的所有内容。
这是在Tomcat5.5下运行。

The thing is that I do not want to use the DailyRollingFileAppender. Reason is that there will be another script that runs daily that will backup everything in the logs folder. This is running under Tomcat5.5.

这可能在log4j中吗?

Is this possible in log4j?

推荐答案

我认为你可以在代码中设置一个系统属性来包含当前日期:

I think you could just set a system property in code to contain the current date:

static{
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    System.setProperty("current.date", dateFormat.format(new Date()));
}

然后在log4j.xml文件中,您可以在指定时使用系统属性在附加器日志文件名:

Then in your log4j.xml file you can use the system property when specifying the log file name in the appender:

<appender name="MYAPPENDER" class="org.apache.log4j.FileAppender">
    <param name="File" value="${user.home}/myApp-${current.date}.log" />

ETA:现在考虑一下,您可能需要使用静态初始化程序设置系统属性确保在配置log4j之前设置了属性。

ETA: Now that I think about it you may have to setup the system property using a static initializer to make sure the property is set before log4j is configured.

这篇关于Log4j中包含日期的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆