Log rotation
Recently I got a problem for log rotation.
We run an open source service on our cloud, and it works well for the functions, but for engineering side, it does not suppport log rotation.
And furthermore, the maintainer think the function is trivial and don't even plan to support this function.
We have at least two solutions for this problem, a) simply change the source code, and add this functionality, and b) use external tools.
The solution a) is straightforward, and we only need to add file size and number check when write logs to file.
But it will involve merge problems when upstream updates, and not even metion the code license problem. So solution a) is my last one.
For solution b), the most common one is to use logrotated service on Linux servers.
Logrotated runs periodically and when file size reaches the size, it renames the file, and sends signal to the process to create new file, and remove old log files.
The very popular nginx use this solution for log rotation.
To use this solution, the service must support signals for log re-create, while my one doesn't.
Until I found multilog, my problem is solved.
Multilog is like tool tee which read from stdout and write to stdout and files, it reads from stdout and write to files with more features such as filer, timestamp adding, etc.
My service doesn't support logrotation, but it can write logs on stdout.
With this I can use the below command to archive the goal of logrotation:
# s<size bytes>: max size for file, 100mb for this case
# n<file number>: keep at least <number> of backups
my_app 2>&1 | multilog s100000000 n10 /var/log/app
Multilog is part of daemontools, which is a nice package, and worth a look.
#log #multilog #daemontools