Home Technology Rotating Log Files with Easylogging++

Rotating Log Files with Easylogging++

by Al

I was looking for a logger to use in some of my C++ projects and came across Easylogging++. It’s pretty easy to use and has a decent feature set, but like so many open source projects it’s a bit lacking in the documentation area. I wanted to set it up to do log rotation when the log file exceeded a given size but how to do this was not really clear. I played around with it this morning and finally figured out how accomplish what I wanted to do, so I’m sharing the code here in case anyone else is trying to solve the same problem.

First the setup process:

  1. Download EasyLogging++ from the repository on GitHub at https://github.com/zuhd-org/easyloggingpp#configuration
  2. Unzip the file and copy the easylogging++.cc and easylogging++.hh into your projects source directory.
  3. Right-click on your project’s header file directory and select ‘Add > Add Existing Item’ and select easylogging++.hh.
  4. Right-click on your project’s source file directory and select ‘Add > Add Existing Item’ and select easylogging++.cc.
  5. Right-click on your project and select ‘Properties’ then select ‘C/C++’ > ‘Preprocessor’. Add ‘ELPP_THREAD_SAFE’ to the ‘Preprocessor Definitions’ section. Make sure you do this for both the Debug and Release sections.
  6. At the top of your main source file add the text ‘INITIALIZE_EASYLOGGINGPP’ to initialize the logger.

That’s it for the setup. The developer did a really nice job with this. It couldn’t be easier to setup.

To actually use the logger in your code, you will need to configure the logger. That can be accomplished by loading a config file and calling reconfigureAllLoggers as follows:

   const el::Configurations conf(R"(logger.conf)");



The config file that I used is as follows:

    FORMAT = “%datetime %thread %level %msg”
    FILENAME = “D:\Win10_Workspaces\LogTest\LogTest\Logs\Logs.log”
    ENABLED = true
    TO_FILE = true
    MAX_LOG_FILE_SIZE = 2097152 ## 2MB

I won’t describe it here because the Easylogging++ site does a pretty good job explaining it. For the purpose of this tutorial though the important line is the MAX_LOG_FILE_SIZE. When your log file exceeds this value a new log file will be generated and the old one will be archived.

To setup the software up for rotating logs you will need to install a callback function to do the job. That’s accomplished using the call:


This function will be called any time your file size hits the MAX_LOG_FILE_SIZE limit that you’ve set in the config file.

The callback function that I put together is as follows:

void PreRollOutCallback(const char* fullPath, std::size_t s)
   char newPath[256];


   std::string str(fullPath);

   std::string pathNoExtension = str.substr(0, str.find_last_of('.'));

   sprintf_s(newPath, sizeof(newPath), "%s_%s_%d.log", pathNoExtension.c_str(), GetDateTime().c_str(), ver);

   rename(fullPath, newPath);

All it really does is rename the existing log file with the date and a version number. When the call returns Easylogging++ will generate a new log file with the default name.

And that’s it. I’m including a download for a sample project that does all the above and creates three thread to demonstrate Easylogging++ multi-threading capabilities.

I’d like to give credit to the developers, Zuhd Web Services. They’ve done an excellent job with this and it’s great of them to share their work with others.

Source Files:

Code is signed by Al Carnali

Hash: 59AC89D5FAB6B6207F9EF81E210AF4CE3FA475A4A2A96C8297B2D61BCBC0A9DA

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.