javaspringspring-bootlogginglogback

logging.file.max-history is not working in Spring Boot


I want to delete log files automatically in my Spring Boot apps and use the following settings:

logging:
  file:
    name: './logs/application.log'
    max-size: 10KB # for test purpose keep small
    max-history: 5

Here is the documentation page: https://docs.spring.io/spring-boot/docs/2.1.13.RELEASE/reference/html/boot-features-logging.html

However, neither max-size nor max-history is working and new log files are created more than 5.

I am not sure if I have to set logback config, but if I have to, is the following approach suitable for deleting log files automatically?

https://www.codejava.net/frameworks/spring-boot/logback-rolling-files-example


Solution

  • I think there is misunderstanding about what the properties mean.

    According to https://www.codejava.net/frameworks/spring-boot/logback-rolling-files-example#

    max-size

    Sets the maximum size of one file, if the file size gets exceeded a new file will be created.

    max-history

    Sets the number of logs to keep. Older files will be deleted, if clean-history-on-start is set to true.

    total-size-cap

    Sets the limit for the total size of all log files.

    You may instead consider this:

    logging:
      file:
        name: './logs/application.log'
        max-history: 5
        total-size-cap: 10KB
        clean-history-on-start: true
    

    for further reference: Logback Documentation