I looked at some other apps and apparently there is a postrotate
option and a copytruncate
option. The copytruncate option is easier but I think it is less reliable as buffers may not be flushed (ie follow @Jarek Potuik answer) and even if they are you may get duplicate entries or dropped entries.
Thus lets assume you want the postrotate
option:
The postrotate
option allows you to run a command to tell your application to reopen the file (close and then open). While opening and closing the file in Java you would do this with a synchronized block or some sort of lock and of course flush any buffers.
Lets assume your app is called myapp
:
You would have a file in /etc/logrotate.d/myapp
with something like:
/var/log/myapp/*.log {
weekly
missingok
rotate 20
compress
delaycompress
notifempty
sharedscripts
postrotate
/etc/init.d/myapp rotate-logs > /dev/null
endscript
}
The command /etc/init.d/myapp rotate-logs
would need to signal to the app to close and reopen the log files.
The signaling part is fairly tricky and depends on what kind of app you have because Java doesn't support IPC that well (ie unix signals) so you may have to open a port or use an existing port such as 8080 (ie HTTP REST command in the case of a servlet container) to tell your app to close and reopen the log files.