[logback-user] dynamically creating/destroying logging appenders

Bridges, Ed (Citco) EBridges at citco.com
Fri Aug 7 23:11:18 CEST 2009


Hi Ceci,

Thanks for the prompt reply!

Just so that we're clear, what I'm talking about is a legacy PSVM program that gets invoked in batch.  What I've been tasked with is having each individual execution get written to its own file.  The SiftingAppender seems to not fit since there is no difference in the different log messages except the time of execution, and the executions would happen sequentially (no parallel executions since the legacy application is not thread safe).

So what I'm looking for is that each execution a new log file would be created depending on some parameters passed in and a timestamp, and then after the execution is completed no more writes would go to that log file.

Thanks!
Ed

-----Original Message-----
From: logback-user-bounces at qos.ch [mailto:logback-user-bounces at qos.ch] On Behalf Of Ceki Gulcu
Sent: Friday, August 07, 2009 4:47 PM
To: logback users list
Subject: Re: [logback-user] dynamically creating/destroying logging appenders

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-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
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Logback-user mailing list
> Logback-user at 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
_______________________________________________
Logback-user mailing list
Logback-user at qos.ch
http://qos.ch/mailman/listinfo/logback-user

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



More information about the Logback-user mailing list