
Hello Ed, As mentioned in my comment, if you need to create just a single appender at application launch, then what you have done will probably work OK. However, if you need to create appenders via configuration file, then the programmatic approach you outlined will require a lot more workt. Moreover, if you need to create multiple file appenders, one for each thread, then things are going to get even more complicated. As long as you stick to programmatic configuration, and a single thread per application, you should be fine. In any case, SiftingAppender provides an elegant solution tailored exactly for the kind of problem you're facing. Cheers, Bridges, Ed (Citco) wrote:
Hello,
Following up from a comment from Ceci on StackOverflow:
http://stackoverflow.com/questions/1239227/dynamically-creating-destroying-l...
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
------------------------------------------------------------------------
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user
-- Ceki Gülcü Logback: The reliable, generic, fast and flexible logging framework for Java. http://logback.qos.ch