Adding a timestamp to a FileAppender file

Hi, I have just switched from log4j to logback. I would like to generate log containing results output by my program, and I want the file name to be SomePrefix.<timestamp>.someExt. I can do this programmatically but I'd like, if possible, to have all my logging configuration in my logback.xml file. Here's the appender I declared : <appender name="resultFile" class="ch.qos.logback.core.FileAppender"> <file>${logFolder}/SomePrefix.%d.someExt</file> <append>false</append> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>info</level> </filter> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%m%n</pattern> </layout> </appender> However the %d pattern is not interpreted, is there a way to do this? Maybe using a RollingFileAppender, but I don't see how. Regards, Nicolas Avant d'imprimer, pensez � l'environnement. Consider the environment before printing this mail.

Hi Nicolas, as far as i understand your goal, a RollingFileAppender would do the job. The following appender is an example of a RollingFileAppender, which does a Rollover every hour with de defined name-pattern. The rollover-policy is defined by the time-pattern: <appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>logFile.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logFile.%d{yyyy-MM-dd_HH}.log</FileNamePattern> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern> </layout> </appender> cheers Hannes ________________________________ Von: logback-user-bounces@qos.ch [mailto:logback-user-bounces@qos.ch] Im Auftrag von nicolas.giraud@bnf.fr Gesendet: Donnerstag, 30. Oktober 2008 11:09 An: logback-user@qos.ch Betreff: [logback-user] Adding a timestamp to a FileAppender file Hi, I have just switched from log4j to logback. I would like to generate log containing results output by my program, and I want the file name to be SomePrefix.<timestamp>.someExt. I can do this programmatically but I'd like, if possible, to have all my logging configuration in my logback.xml file. Here's the appender I declared : <appender name="resultFile" class="ch.qos.logback.core.FileAppender"> <file>${logFolder}/SomePrefix.%d.someExt</file> <append>false</append> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>info</level> </filter> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%m%n</pattern> </layout> </appender> However the %d pattern is not interpreted, is there a way to do this? Maybe using a RollingFileAppender, but I don't see how. Regards, Nicolas Avant d'imprimer, pensez à l'environnement. Consider the environment before printing this mail.

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 Avant d'imprimer, pensez � l'environnement. Consider the environment before printing this mail.

Helo Nicolas, Have a look at http://logback.qos.ch/xref-test/ch/qos/logback/core/appender/FileAppenderTes... 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@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

I would suggest you create a Jira issue for this. I would suggest the ability to add a custom pattern resolver to the appender might be what you want. Before Logback I had written my own logging framework that supported this. I had considered making this request myself but so far I am not sure that we actually need it yet. It could look something like: <appender name="file" class="ch.qos.logback.core.FileAppender"> <File resolver="com.mycorp.logback.MyPatternResolver">myjob.%{host}.%{datetime}.log</File> <Append>false</Append> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%msg%n</Pattern> </layout> </appender> The Resolver interface would have a single method that looks like String resolve(String pattern) Ralph nicolas.giraud@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
*Avant d'imprimer, pensez à l'environnement.* Consider the environment before printing this mail. ------------------------------------------------------------------------
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user

A jira issue sounds good. Given that the RollingFileAppender already inherits from FileAppender and is getting pretty complicated to maintain, I would prefer a new class, e.g. ExtFileAppender. At the same time, since the idea of a resolver also makes sense in RollingFileAppender, FileAppender might still be the right place. It's a case of definitely, maybe. Ralph Goers wrote:
I would suggest you create a Jira issue for this. I would suggest the ability to add a custom pattern resolver to the appender might be what you want. Before Logback I had written my own logging framework that supported this. I had considered making this request myself but so far I am not sure that we actually need it yet. It could look something like:
<appender name="file" class="ch.qos.logback.core.FileAppender"> <File resolver="com.mycorp.logback.MyPatternResolver">myjob.%{host}.%{datetime}.log</File> <Append>false</Append> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%msg%n</Pattern> </layout> </appender>
The Resolver interface would have a single method that looks like String resolve(String pattern)
Ralph
nicolas.giraud@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
*Avant d'imprimer, pensez à l'environnement.* Consider the environment before printing this mail. ------------------------------------------------------------------------
_______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user
_______________________________________________ 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
participants (4)
-
Ceki Gulcu
-
Lang, Johannes
-
nicolas.giraud@bnf.fr
-
Ralph Goers