Put the date (e.g. yyyy-MM-dd) in the log file output by logback

When I use logback to output a log to a file ... I wondered if I could put the date in the active log file, and I found out that I could do it normally!

What should i do?

Simply omit the file property of the RollingFileAppender and include the date format of the unit you want to rotate in the fileNamePattern property of the TimeBasedRollingPolicy.

<appender name="APPLICATION_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!--<file>${app.log.dir:-log}/application.log</file>--> <!--Omit this! !!-->
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${app.log.dir:-log}/application-%d{yyyy-MM-dd}.log</fileNamePattern>
        <maxHistory>7</maxHistory>
    </rollingPolicy>
    <encoder>
        <charset>UTF-8</charset>
        <pattern><![CDATA[date:%d{yyyy-MM-dd HH:mm:ss}\tthread:%thread\tX-Track:%X{X-Track}\tlevel:%-5level\tlogger:%-48logger{48}\tmessage:%msg%n]]></pattern>
    </encoder>
</appender>

Is this a formal method?

"TimeBasedRollingPolicy fileNamePattern property ”Was described exactly in the explanation column!

Summary

There is nothing to summarize, but ... It's a hassle to look at the document, but if it's written properly, you can use it with confidence! !! Especially ... Documentation is important so that many people can use it as OSS. It is also good that logback has a Japanese translation !! (Thanks to the translator)

Recommended Posts

Put the date (e.g. yyyy-MM-dd) in the log file output by logback
Utilization of seed file
Check file size before deploying
About size comparison of compareTo
List of hosts file locations
Put the date (e.g. yyyy-MM-dd) in the log file output by logback
Format of the log output by Tomcat itself in Tomcat 8
Log output to file in Java
Output log to external file with slf4j + logback with Maven
Sample code for log output by Java + SLF4J + Logback
Put the file in the properties of string in spring xml configuration
Output relative path in .classpath file generated by gradle eclipse
Logback log is not output when the server application is closed
Unzip the zip file in Java
Enable log output to both file and console using log4j in Eclipse.
Add the date to the GC statistics acquired by gcutil and output it.
How to get the current date as a string in yyyyMMdd format
I checked the package name referenced by the class contained in the JAR file