Hi.
I was looking for an appender that would create the
new log file
on each start of an application, with the additional
request that the
file name must contain the current timestamp.
While I was unsuccessfully looking for the appender on
the net,
I've also found that other people search for the same
thing.
Finally, I have decided to write the appender myself.
I have tested it, it works fine, and now I am willing
to share it.
Appender is pretty simple, it is derived from
FileAppender,
it has the additional functionality that it replaces
"{timestamp}"
string from the file name with the current timestamp.
Timestamp pattern is user configured by
TimestampPattern property that
has the same choice of formats as
java.text.SimpleDateFormat
So, I find the solution very flexible.
I have attached the following files:
TimestampFileAppender.java
Test.java
log4j.configuration
... the actual appender, simple test class and
configuration file.
I am waiting for your feedback.
Kind regards, Viktor.
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.log4j.FileAppender;
/**
* @author Viktor Bresan
*
*/
public class TimestampFileAppender extends FileAppender {
private static final String TARGET = "\\{timestamp\\}";
protected String timestampPattern = null;
/**
*
*/
public void setFile(String file) {
if (timestampPattern != null) {
super.setFile(file.replaceAll(TARGET, new SimpleDateFormat(timestampPattern).format(Calendar.getInstance().getTime())));
} else {
super.setFile(file);
}
}
/**
*
* @param fileName
* @param append
*/
public void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) throws IOException {
if (timestampPattern != null) {
super.setFile(fileName.replaceAll(TARGET, new SimpleDateFormat(timestampPattern).format(Calendar.getInstance().getTime())), append, bufferedIO, bufferSize);
} else {
super.setFile(fileName, append, bufferedIO, bufferSize);
}
}
/**
*
* @return
*/
public String getTimestampPattern() {
return timestampPattern;
}
/**
*
* @param timestampPattern
*/
public void setTimestampPattern(String timestampPattern) {
this.timestampPattern = timestampPattern;
}
}
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Test {
private static Logger _logger = Logger.getLogger(Test.class);
/**
* @param args
*/
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.configuration");
_logger.debug("Debug message.");
_logger.info("Info message.");
_logger.error("Error message.");
_logger.fatal("Fatal message.");
}
}
# Set root logger level to DEBUG and its appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a TimestampFileAppender.
# TimestampPattern uses SimpleDateFormat pattern.
log4j.appender.A1=TimestampFileAppender
log4j.appender.A1.TimestampPattern=yyyy_MM_dd__HH_mm_ss_SSS
log4j.appender.A1.File=.\\log\\test.{timestamp}.log
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %C{1}.%M %x - %m%n
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]