Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

java - Filename with date in 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

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.

Is this possible in log4j?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()));
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...