[logback-user] file appender help needed

Ceki Gulcu ceki at qos.ch
Wed Aug 13 11:35:19 CEST 2008

Hello Chris,

I think logs/yyyy/MM/dd/appname.log structure would be useful for other users as 
well. Please create an issue and we'll add support for this structure. If you 
can't wait, you may want to send in a patch as well. Wink, wink.

Chris Cheshire wrote:
> I want to configure logback such that logs are written to the directory
> logs/yyyy/MM/dd/immigrator.log
> with the directory structure created each day.
> I have :
>   <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
>     <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
>       <FileNamePattern>logs/%d{yyyy/MM/dd/}immigrator.log</FileNamePattern>
>     </rollingPolicy>
>     <layout class="ch.qos.logback.classic.PatternLayout">
>       <Pattern>%-25(%date{HH:mm:ss.SSS} [%thread]) %-5level
> %logger{10} - %msg%n</Pattern>
>     </layout>
>   </appender>
> The first time the app is run, I see the following :
> 14:34:13,536 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy -
> Adding parent to RollingPolicy: FILE
> 14:34:13,543 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No
> compression will be used
> 14:34:13,544 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy -
> Will use the pattern logs/%d{yyyy/MM/dd/}immigrator.log for the active
> file
> 14:34:13,557 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - The
> date pattern is 'yyyy/MM/dd/' from file name pattern
> 'logs/%d{yyyy/MM/dd/}immigrator.log'.
> 14:34:13,557 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy -
> Roll-over at midnight.
> 14:34:13,563 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy -
> Generated a new name for RollingFileAppender:
> logs/2008/08/12/immigrator.log
> 14:34:13,563 |-INFO in
> ch.qos.logback.core.rolling.RollingFileAppender[FILE] - Active log
> file name: logs/2008/08/12/immigrator.log
> 14:34:13,564 |-ERROR in
> ch.qos.logback.core.rolling.RollingFileAppender[FILE] -
> setFile(logs/2008/08/12/immigrator.log,true) call failed.
> java.io.FileNotFoundException: logs/2008/08/12/immigrator.log (No such
> file or directory)
> java.io.FileNotFoundException: logs/2008/08/12/immigrator.log (No such
> file or directory)
>         at java.io.FileOutputStream.openAppend(Native Method)
>         at java.io.FileOutputStream.<init>(FileOutputStream.java:177)
>         at java.io.FileOutputStream.<init>(FileOutputStream.java:102)
>         at ch.qos.logback.core.FileAppender.setFile(FileAppender.java:142)
>         at ch.qos.logback.core.FileAppender.start(FileAppender.java:99)
>         at ch.qos.logback.core.rolling.RollingFileAppender.start(RollingFileAppender.java:57)
> (stack trace truncated for brevity).
> This creates the directory structure properly, but the initial log
> file is not created. If I then run the app again, then a log file is
> created in the date-based directory.
> If I add a <File>logs/immigrator.log</File> element to the appender,
> then it creates the log file outside of the date structure and only
> rolls it at the end of the day. This is not what I want. I want all my
> log files automatically written to the "logs/yyyy/MM/dd/" directory
> only, not in a staging area then moved at the end of the day. I tried
> putting the pattern into the file element but I got a similar
> exception with logback trying to create the file name exactly as it
> was specified without expanding the date formats.
> I looked into using variable substitution but this requires me to set
> the log directory in the java code itself. I could do this by creating
> a ${log.dir} system property and having the app create the date based
> structure before anything else is done, but I don't want to have to do
> this in every system app I write (this is the route I went with
> log4j). It would be a lot easier if logback took over this part of
> things purely in the configuration.
> Is this possible?
> Chris
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://qos.ch/mailman/listinfo/logback-user

Ceki Gülcü

QOS.ch is looking to hire talented developers located in Switzerland
to work on cutting-edge software projects. If you think you are
qualified, then please contact ceki at qos.ch.

More information about the Logback-user mailing list