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.
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.
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.
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.
"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.
The documentation for TimeBasedRollingPolicy
states:
Note that the
file
property inRollingFileAppender
(the parent ofTimeBasedRollingPolicy
) can be either set or omitted. By setting the file property of the containingFileAppender
, 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 thefile
property. It follows that the name of the currently active log file will not change over time. However, if you choose to omit thefile
property, then the active file will be computed anew for each period based on the value offileNamePattern
.
In your case, just omit the file
property.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With