WL#8988: Router - Add log rotation on Unix systems
Motivation
Allow integration with external log-rotation applications by reopening the file-based logfile on SIGHUP.
On Linux this allows to integration with the system-wide "logrotate".
Goal
- Handle SIGHUP by reopening log-files.
- FR1
- It should be possible to trigger log rotation on demand while the Router is running
- FR2
- When the log rotation (that forces Router to reopen the logfile) is requested and the Router has lost the access rights to the log file in the meantime, the Router should gracefully close with exit code 1, printing proper error on the console (logging file is not accessible).
Rotation On Demand
It should be possible to induce log rotation on demand.
It would be accomplished in two steps (either directly from shell or from script, which could be called automatically as a scheduled task):
- rename the log file
- notify Router (so it creates and switches to a new log file)
logrotate
Logrotate mechanisms would be used to rotate the log file.
After rotating, Router would be notified to reopen the log file, and this would be accomplished via sending HUP to Router process.
logrotate
https://linux.die.net/man/8/logrotatekill -HUP <Router pid>
Periodic Rotation, Size-based Rotation
Given that the log file can be rotated on demand from shell level as described above, implementing time- or size-based rotation is just a matter of writing a script that will run periodically and rotate the log file when criteria are met.
This script would be ran by operating system's native scheduler (cron on Unix).
In other words, we can delegate all this work to a system administrator.
Rotation on Start
Again, this can be done from shell level. Simply rename the file before starting the router, in the startup script.