Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux/c++ log rotation scheme

I have a logger system which basically is a fancy way of writing my data to std::clog in a thread safe way.

I also, redirect std::clog to a file like this:

int main() {
    std::ofstream logfile(config::logname, std::ios::app);
    std::streambuf *const old_buffer = std::clog.rdbuf(logfile.rdbuf());

    // .. the guts of the application

    std::clog.rdbuf(old_buffer);
}

This works great... however, my application also produces a very large amount of logs. I was wondering what would be a good way to properly rotate my log files. Is there a safe way to switch out the file via a cron task? I would guess no.

The only thing I can think of that would definitely work is if I had the application itself open a new file, and redirect the rdbuf of clog to that while holding the logging mutex. But that feels like a cheap solution, and I would need to check so see if it is time to rotate logs fairly often for it to be effective. There has got to be a better way.

like image 445
Evan Teran Avatar asked Nov 30 '22 20:11

Evan Teran


1 Answers

You can use the built-in log rotation method configured in /etc/logrotate.conf and/or /etc/logrotate.d/ - it's common to have logrotate send your app a SIGUSR1 as a signal to close and re-open all your log files.

like image 124
Paul Tomblin Avatar answered Dec 06 '22 20:12

Paul Tomblin