I am writing a Linux daemon that writes a log. I'd like the log to be rotated by logrotate. The program is written in C.
Normally, my program would open the log file when it starts, then write entries as needed and then, finally, close the log file on exit.
What do I need to do differently in order to support log rotation using logrotate? As far as I have understood, my program should be able to reopen the log file each time logrotate has finished it's work. The sources that I googled didn't, however, specify what reopening the log file exactly means. Do I need to do something about the old file and can I just create another file with the same name? I'd prefer quite specific instructions, like some simple sample code.
I also understood that there should be a way to tell my program when it is time to do the reopening. My program already has a D-Bus interface and I thought of using that for those notifications.
Note: I don't need instructions on how to configure logrotate. This question is only about how to make my own software compatible with it.
logrotate is a log managing command-line tool in Linux. Through the configuration file, logrotate will execute the appropriate function to manage the matching log files. Although logrotate works for any kind of file, it's generally used for managing the log files.
Logrotate uses configuration files to set rules for the rotation's behavior, such as the maximum log files to keep, how often the rotation will run, and the log file's size to compress. Logrotate's configuration files are located in /etc/logrotate. conf file and /etc/logrotate.
The main logrotate configuration file is located at /etc/logrotate. conf . The file contains the default parameters that logrotate uses when it rotates logs.
There are several common ways:
logrotate
and your program should be able to catch a signal (usually SIGHUP) as a request to close and reopen its log file. Then logrotate
sends the signal in a postrotate scriptlogrotate
and your program is not aware of it, but can be restarted. Then logrotate
restarts your program in a postrotate script. Cons: if the start of the program is expensive, this may be suboptimalyou use logrotate
and your program is not aware of it, but you pass the copytruncate option to logrotate
. Then logrotate
copies the file and then truncates it. Cons: in race conditions you can lose messages. From rotatelog.conf
manpage
... Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost...
you use rotatelogs
, an utility for httpd Apache. Instead of writing directly to a file, you programs pipes its logs to rotatelogs
. Then rotatelogs
manages the different log files. Cons: your program should be able to log to a pipe or you will need to install a named fifo.
But beware, for critical logs, it may be interesting to close the files after each message, because it ensures that everything has reached the disk in case of an application crash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With