Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNamePattern in RollingFileAppender - logback Configuration

Tags:

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.

like image 428
Aks Avatar asked Jun 08 '12 18:06

Aks


People also ask

What is RollingFileAppender in Logback?

Logback RollingFileAppender appends log events into a file with the capability to rollover (archive the current log file and resume logging in a new file) based on a particular schedule, such as daily, weekly, monthly or based on log file size.

What is RollingPolicy in Logback?

A RollingPolicy is responsible for performing the rolling over of the active log file. The RollingPolicy is also responsible for providing the active log file, that is the live file where logging output will be directed.

What is Max history in Logback?

The maxHistory property controls the maximum number of archive files to keep, deleting older files. For example, if you specify monthly rollover, and set maxHistory to 6, then 6 months worth of archives files will be kept with files older than 6 months deleted.

What is root level in Logback?

"Root" level does not restrict levels of other loggers, it merely sets the default. So <root level="INFO"> and <logger name="some.name" level="DEBUG"> are perfectly suitable together, and you don't need to relax the "root" level. So both examples should log on debug level for logger named com.


2 Answers

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.

like image 179
Ceki Avatar answered Sep 22 '22 08:09

Ceki


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> 
like image 41
Przemek Nowak Avatar answered Sep 21 '22 08:09

Przemek Nowak