FileNamePattern in RollingFileAppender - logback Configuration

Aks picture Aks · Jun 8, 2012 · Viewed 26.4k times · Source

I have the following RollingFileappender in my logback configuration file.

<appender name="RollingFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <File>C:\Files\MyLogFile.log</File>      
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
  <FileNamePattern>C:\Files\MyLogFile.%d{yyyy-MM-dd}.log</FileNamePattern>       
  <MaxHistory>30</MaxHistory>     
  </rollingPolicy>      
  <encoder>
      <pattern>%date %level [%thread] %logger{60} [%file:%line] %msg%n</pattern>
    </encoder> 
  </appender>

It does write a file to the above directory as MyLogFile.log but does not append the date as specified in the FileNamePattern. Any ideas how can I manage to append the date in my fileName. Thanks.

Answer

Ceki picture Ceki · Jun 9, 2012

The documentation for TimeBasedRollingPolicy states:

Note that the file property in RollingFileAppender (the parent of TimeBasedRollingPolicy) can be either set or omitted. By setting the file property of the containing FileAppender, you can decouple the location of the active log file and the location of the archived log files. The current logs will be always targeted at the file specified by the file property. It follows that the name of the currently active log file will not change over time. However, if you choose to omit the file property, then the active file will be computed anew for each period based on the value of fileNamePattern.

In your case, just omit the file property.