Log rotation in Spring Boot service

2020-07-27 03:42发布

问题:

I am deploying a Spring Boot 2.0.0-RC1 application as an init.d service, but I can't figure out how to configure the log rotation. The app logs to /var/log/appname.log, but if I configure logrotate the logging stops after a rotation, because a new file is created, and the stdout/stderr redirection defined in the embedded script does not work anymore.

If I configure the log rotation in my logging system there are two problems: I can't create the files in /var/log, and I still have the redirection defined in the embedded script.

What is the proper solution for this?

回答1:

I'm facing the same problem in several applications and adding copytruncate param is the solution because your Spring Boot application doesn’t understand that the file has changed (truncated) and is acting like a tail -f command (see How does the “tail” command's “-f” parameter work? for details).

Example:

/opt/payara41/glassfish/domains/domain1/logs/* {
    daily
    copytruncate
    rotate 3
    dateext
    notifempty
}

copytruncate

Truncate the original log file to zero size in place after creating a copy, instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place



回答2:

I found the solution, it's the option copytruncate in logrotate.