How to include date in log file's name with Spring Boot / slf4j?

15,612

As described here

Spring Boot has a LoggingSystem abstraction that attempts to configure logging based on the content of the classpath.

To employ it

The simplest way to do that is through the starter poms which all depend on spring-boot-starter-logging. For a web application you only need spring-boot-starter-web since it depends transitively on the logging starter.

enter image description here

Because Logback is available it is the first choice.

To configure the more fine-grained settings of a logging system you need to use the native configuration format supported by the LoggingSystem in question. By default Spring Boot picks up the native configuration from its default location for the system (e.g. classpath:logback.xml for Logback), but you can set the location of the config file using the "logging.config" property.

If default is ok for you just create logback.xml and add corresponding file appender, e.g.

<appender name="rollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <FileNamePattern>LogFile.%d{yyyy-MM-dd}.log</FileNamePattern>
    <MaxHistory>30</MaxHistory>
  </rollingPolicy>
  <encoder>
    <pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>
  </encoder>
</appender>

Additional documentation could be found here

Share:
15,612
sinedsem
Author by

sinedsem

I'm from Russia, so I can do mistakes

Updated on June 26, 2022

Comments