1

I have some logs that I want to write to a file in a spring-boot microservice.

In my "application.yml" I have the below line which is working fine. But I also want to add date and time to the file name. How do I do that?

logging:
  file: logs/${spring.application.name}.log

Ideally, I want the filename to be something like this:

logs/spring-boot-application_YYYY_MM_DD_HH_MM

2 Answers2

0

A similar question has been answered here: How to include date in log file's name with Spring Boot / slf4j?

Basically if you want more control over the logging setup, you should have your own logback.xml under src/main/resources and configure the naming format similar to:

<FileNamePattern>LogFile.%d{yyyy-MM-dd}.log</FileNamePattern>
Michael
  • 556
  • 3
  • 12
0

If you are using spring-boot , you could easily specify it in application.yml or application.properties and configuring logback xml to enable Rolling File Appender as :

spring:
logging:
  file: logs/dev_app.log
  pattern:
    console: "%d %-5level %logger : %msg%n"
    file: "%d %-5level [%thread] %logger : %msg%n"
  level:
    org.springframework.web: DEBUG
    guru.springframework.controllers: DEBUG
    org.hibernate: DEBUG

and specify the time based rolling policy for the file in :

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

  <appender name="ROLLIN" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_FILE}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

        <!-- daily rollover -->
        <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>

    </rollingPolicy>
  </appender>

  <root level="INFO">
    <appender-ref ref="ROLLIN" />
  </root>

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

Doc Official Spring Doc

Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39