9

Currently my Spring-boot application logs to a file named: myLog.log, this is working as intended, however I would like the log file to have a timestamp at the end of it and create a new file each time it is ran.

I have tried to implement this in my logback-test.xml file shown below, but it is just giving me the filename: myLog.log without a timestamp.

How can I fix this?

Logback-test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <logger name="org.springframework.web" level="INFO"/>

    <!-- Send debug messages to System.out -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS}  - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>path/to/my/file/mylog.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>mylog.%i{yyyy-MM-dd_HH:mm:ss.SSS}}.log</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>10</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>2MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <logger name="com.my.package" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </logger>

    <!-- By default, the level of the root level is set to DEBUG -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>
java123999
  • 6,974
  • 36
  • 77
  • 121

1 Answers1

24

You can define a variable like this:

<timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>

(note: don't use colons in datePattern)

Then use it directly in your appender's file element:

 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>path/to/my/file/mylog-${myTimestamp}.log</file>
 ...
 </appender>

Or in a simple FileAppender:

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>path/to/my/file/mylog-${myTimestamp}.log</file>
    <encoder>
        <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
    </encoder>
</appender>
fglez
  • 8,422
  • 4
  • 47
  • 78
nyname00
  • 2,496
  • 2
  • 22
  • 25
  • Thanks, this is working but I have to replace {timestamp} with {myTimestamp}. It does, however, create two log files. How can I solve this? – java123999 Apr 22 '16 at 14:30
  • 1
    Please see my following question: http://stackoverflow.com/questions/36796728/logback-test-xml-configuration-is-producing-two-log-files-instead-of-one – java123999 Apr 22 '16 at 14:38
  • `myTimestamp` ofc. fixed that – nyname00 Apr 22 '16 at 14:48