FileNamePattern in RollingFileAppender - logback Configuration
Asked Answered
D

2

23

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.

Dirkdirks answered 8/6, 2012 at 18:9 Comment(1)
This is a good question and confused me too. LogBack keeps logging to the MyLogFile.log and never ends up in the MyLogFile.%{yyyy-MM-dd}.log I wonder what the use is for this file property.Glossitis
A
29

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.

Abscission answered 9/6, 2012 at 8:5 Comment(1)
example on webpage logback.qos.ch/manual/appenders.html is broken, it uses both file and fileNamePatternOgdoad
D
3

For example you can use the following configuration. It was tested and works :)

<!-- FILE APPENDER WITH PRUDENT MODE -->
<!-- IN PRUDENT MODE CANNOT BE SPECIFIED FILE, THIS PARAM IS OBTAINED FROM FILE NAME PATTERN -->
<!-- IN PRUDENT MODE ONLY TIME BASED ROLLING POLICY IS SUPPORTED - BECAUSE WE HAVE A LOG OF MULTIPLE JVM INSTANCES-->
<!-- SEE MORE AT http://logback.qos.ch/manual/appenders.html#prudentWithRolling -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <prudent>true</prudent>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${logfile.path}-%d{yyyy-MM-dd}.log</fileNamePattern>
    </rollingPolicy>

    <encoder>
        <pattern>${HOSTNAME} %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>
Disentangle answered 26/2, 2014 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.