10

It is the same question as Setting a log file name to include current date in Log4j , but how to apply it to Spring Boot, which comes with slf4j?

application.properties

spring.application.name=keywords
logging.file=logs/${spring.application.name}.log
Community
  • 1
  • 1
sinedsem
  • 5,413
  • 7
  • 29
  • 46
  • logging.file was deprecated and renamed to logging.file.name in 2.2. It was then removed in 2.3. – Andrew Feb 13 '22 at 05:01

1 Answers1

4

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

Mike Shauneu
  • 3,201
  • 19
  • 21