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 to MyLogFile.log but does not append the date as specified in the FileNamePattern.

There is a simular question, but it never answered my confusion.

I should omit the file property. Why is the file-property then usefull, because it seems to overrule the rolling-file activity.

This confuses me. Did is get something wrong?

有帮助吗?

解决方案

The file property, when used along the fileNamePattern one, will define the name of the ACTIVE file, and only archived files will have that pattern as name. When the active file is rolled, a new archive file will be created with that name. From chapter 4 in the manual:

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.

So it is useful when you want to have a fixed name, for example, if you need to send the active file programmatically and don't want to guess what the file name would be for the current date.

Also notice that file is a property inherited from the regular FileAppender.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top