0

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

10
  • Just to be clear: max-history determines not the amount of files but the number of days to keep the logs, so in your case everything older than 5 days will be deleted.
    – leun4m
    Commented Mar 2, 2023 at 14:40
  • Hmm, good information. Then I have 2 questions. 1. Will the delete operation occurs only initializing the app?
    – user21263160
    Commented Mar 2, 2023 at 14:42
  • 2. Do you have an idea how can I solve this problem? O I have to use max-history and wait the log files to be deleted by older than x days ?
    – user21263160
    Commented Mar 2, 2023 at 14:43
  • It will do so, if you set logging.file.clean-history-on-start=true
    – leun4m
    Commented Mar 2, 2023 at 14:45
  • If I do not do this? When will it clean or what is the meaning of that setting (max-history)?
    – user21263160
    Commented Mar 2, 2023 at 14:47

2 Answers 2

4

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

2
0

I am using a Java-Maven Project with spring-boot-starter-parent in combination with spring-boot-starter-web both at version 2.7.18. I have added the following lines at my application.properties, but it seems, that there might be some problems. Is there anything to consider while using the 2.x version of spring boot?

UPDATE: i found out, that the line total-size-cap makes the problem. If I comment this out, all existing logfiles are deleted based on max-history and the current logfile will be written. If this line is comment in, no existing log file will be deleted, and there is no current logfile.

Is there any explanation for this?

logging.level.root=INFO
logging.file.name=d:/logpath/logging.log
logging.file.max-history=2
logging.file.total-size-cap=1MB
logging.file.clean-history-on-start=true