[logback-user] Adding a timestamp to a FileAppender file

Ceki Gulcu listid at qos.ch
Thu Oct 30 12:06:35 CET 2008


Helo Nicolas,

Have a look at

http://logback.qos.ch/xref-test/ch/qos/logback/core/appender/FileAppenderTest.html

You would need to write

  FileAppender<Object> appender = new FileAppender<Object>();

as

   FileAppender<LoggingEvent> appender = new FileAppender<LoggingEvent>();

You would probably also want to attach the appender you just created to a 
logger, possibly as

   LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
   ch.qos.logback.classic.Logger rootLogbackLogger =
                                     lc.getLogger(LoggerContext.ROOT);

   rootLogbackLogger.addAppender(the file appender you just created);


Also, the file appender should be attached to logback's logger context.
You should modify the test case, from

  appender.setContext(new ContextBase());

to

  appender.setContext(lc);

Holler if you need further info,

nicolas.giraud at bnf.fr wrote:
> 
> Thanks for the answer, Hannes.
> 
> However a time-based rollover is not what I am looking for. My program 
> executes one job at a time. There are several jobs  whose duration 
> varies from a couple of seconds to several hours.  I simply want to log 
> the results of a job in a single file, and have the filename contain a 
> timestamp.
> 
> I will have to do this programmatically obviously, but I'm having 
> trouble setting up the file appender programmatically.
> 
> Regards,
> Nicolas
> 

-- 
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch


More information about the Logback-user mailing list