Reputation: 572
Have tried to search for it but I keep on getting ...IS_UNDEFINED
as my file name. I simply want to append the current date to a log file. Any simple example for the logback.xml file?
This is my latest try:
<Properties>
<property name="filePattern">log_${date:yyyy-MM-dd}.log</property>
</Properties>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${application.home:-.}/logs/${filePattern}</file>
<encoder>
<pattern>%date [%level] from %logger in %thread - %message%n%xException</pattern>
</encoder>
</appender>
Upvotes: 13
Views: 16407
Reputation: 572
I figured it out like this:
<timestamp key="timestamp" datePattern="yyyyMMdd"/>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${application.home:-.}/logs/log_${timestamp}.log</file>
<encoder>
<pattern>%date [%level] - %message%n%xException</pattern>
</encoder>
</appender>
Upvotes: 17
Reputation: 9355
Regular FileAppender does not support patterns in filenames, so you have to use RollingFileAppender + TimeBasedRollingPolicy instead.
RollingFileAppender extends FileAppender with the capability to rollover log files. For example, RollingFileAppender can log to a file named log.txt file and, once a certain condition is met, change its logging target to another file.
TimeBasedRollingPolicy is both easy to configure and quite powerful. It allows the roll over to be made based on time. It is possible to specify that the roll over occur once per day, per week or per month.
Like this:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${application.home:-.}/logs/log_.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%date [%level] from %logger in %thread - %message%n%xException</pattern>
</encoder>
</appender>
Upvotes: 17