[logback-user] file appender help needed

Chris Cheshire cheshirator at gmail.com
Tue Aug 12 23:47:51 CEST 2008


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


More information about the Logback-user mailing list