[logback-user] dynamically creating/destroying logging appenders
Bridges, Ed (Citco)
EBridges at citco.com
Fri Aug 7 22:31:00 CEST 2009
Hello,
Following up from a comment from Ceci on StackOverflow:
http://stackoverflow.com/questions/1239227/dynamically-creating-destroying-logging-appenders
I'd like to get context around his point that it is not possible, since it appears not only possible but quite easy. Likely I'm just missing something and would like to get more background about how to approach this problem.
I quote here my original question:
I have a legacy PSVM application which I'd like to redirect its logging output to unique files per execution. So, if I invoke it at 10:00, then have it redirect it's output to {thread-id}-10:00.log; and another thread of execution may begin an execution at 10:01, and its output would go to {thread-id}-10:01.log. I understand that this is not elegant.
My questions are:
· is this possible?
· does someone have an idea of how to approach?
· is it possible to release/destroy an appender when it's no longer needed?
A suggestion I received on this posting proposed simply extending FileAppender. Based on that I created a simple (naïve?) class named "DynamicRuntimeFileAppender" like so:
public class DynamicRuntimeFileAppender extends FileAppender {
private static Logger log = Logger.getLogger(DynamicRuntimeFileAppender.class);
public DynamicRuntimeFileAppender() {
}
public DynamicRuntimeFileAppender(
Layout layout,
String filename,
boolean append,
boolean bufferedIO,
int bufferSize) throws IOException {
super(layout, filename, append, bufferedIO, bufferSize);
}
public DynamicRuntimeFileAppender(
Layout layout,
String filename,
boolean append) throws IOException {
super(layout, filename, append);
}
public DynamicRuntimeFileAppender(
Layout layout,
String filename) throws IOException {
super(layout, filename);
}
public static Appender configuredInstance(String filename) throws IOException {
if(null == filename || filename.length() < 1) {
throw new IllegalArgumentException("filename cannot be empty.");
}
HTMLLayout layout = new HTMLLayout();
layout.setTitle(filename);
boolean append = false;
DynamicRuntimeFileAppender appender = new DynamicRuntimeFileAppender(layout, filename, append);
appender.setName(filename);
appender.setBufferedIO(false);
appender.setEncoding("UTF-8");
appender.setImmediateFlush(true);
appender.setThreshold(Level.ALL);
return appender;
}
}
When I run it using a test like this, it appears to work fine:
private static final Logger log = Logger.getLogger(Log4JTest.class);
@Test
public void testAddAppender() throws Exception {
Appender a = DynamicRuntimeFileAppender.configuredInstance(
buildFilename("target/Log4JTest")
);
log.addAppender(a);
log.info(format("test message using appender [%s]", a.getName()));
log.removeAppender(a);
a.close();
}
Thank you very much.
Ed
Disclaimer link. To see it, click the link below, or copy and paste it into your browser's address line.
http://www.citco.com/emaildisclaimer.htm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://qos.ch/pipermail/logback-user/attachments/20090807/74532846/attachment-0001.htm>
More information about the Logback-user
mailing list