codentary
codentary

Reputation: 1175

Boost log 'text_file_backend' - rotate file on demand

I want to cover the cases when my process crashes or just does not shutdown properly and the logs are not rotated.

2 specific cases:

After looking into it I came to the conclusion that I would do the following:

The problem is that rotate_file() has no effect. The log file is not rotated when I call it.
Am I doing something wrong?

Here is how I setup the file sync:

// File log
auto file_sink = bl::add_file_log(bl::keywords::target          = "history",
                                  bl::keywords::file_name       = "app.log"),
                                  bl::keywords::target_file_name= "app_%Y%m%d_%H%M%S_%5N.log"),
                                  bl::keywords::rotation_size   = 25 * 1024 * 1024,
                                  bl::keywords::max_size        = 250 * 1024 * 1024,
                                  bl::keywords::auto_flush      = true,
                                  bl::keywords::open_mode       = std::ios_base::app,
                                  bl::keywords::max_files       = 10);

file_sink->set_formatter(log_fmt);
file_sink->set_filter(bl::trivial::severity >= level);

// In case the previous log was not rotated on shutdown (it's not empty), rotate it now
file_sink->locked_backend()->rotate_file(); // NOT DOING ANYTHING!

For consistency, I was thinking to also disable rotation on shutdown (keywords::enable_final_rotation = false) in order to only rotate log file on startup, regardless of how my app terminated (crash, kill -9, proper shutdown...) but to get to this I first need to be able to rotate logs on startup.

Upvotes: 0

Views: 674

Answers (1)

codentary
codentary

Reputation: 1175

As Andrey Semashev mentioned, in order for rotate_file() to work, you first need to log something so the log file is actually opened.

Taking this into account I do the following during logger init (on app startup):

  • disable rotation on shutdown (keywords::enable_final_rotation = false) - this is so for the cases where we don't have a crash, we don't do an extra empty file rotation (in all cases rotation is done on startup or on hitting the collector limits).
  • open the file for append (bl::keywords::open_mode = std::ios_base::app)
  • log something like "Rotating logger on demand"
  • call rotate_file() - this will do the rotation now that log file is opened.

Here is how it looks:

 // File log
auto file_sink
    = bl::add_file_log(bl::keywords::target                = "history",
                       bl::keywords::file_name             = "app.log",
                       bl::keywords::target_file_name      = "app_%Y%m%d_%H%M%S_%5N.log",
                       bl::keywords::rotation_size         = 10 * 1024 * 1024,
                       bl::keywords::open_mode             = std::ios_base::app, // Open for append and immediately rotate
                       bl::keywords::enable_final_rotation = false // Rotate only on startup to cover also the crash / kill cases
    );

// Log something so the logger opens the file
BOOST_LOG_TRIVIAL(info) << "Rotating logs on startup";

// Do the rotation on demand
file_sink->locked_backend()->rotate_file();

Upvotes: 1

Related Questions