MFC, C++ - Logging, best way

Noah Aas 685 Reputation points
2025-03-06T16:58:11.8966667+00:00

I am looking for the best way to add logging to my application.

What must it be able to do? Configuration of where the files are stored.

  • 2025-03-06-LogFile.log

If the file is larger than 30000 MByte, a new one should be written. Files older than 30 days should be deleted each time the application is started.

Log Level

  • Error
  • Debug
  • Info

Thanks.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,874 questions
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 13,001 Reputation points Microsoft External Staff
    2025-03-07T01:40:19.03+00:00

    Hi,

    This problem is not related to MFC.
    C++ standard library only provides simple I/O functions.

    According to your requirements, it is recommended to search for Log-related open-source libraries on GitHub.
    It's easy to find some sample.

    console_sink->set_level(spdlog::level::warn);

     // Create a file rotating logger with 5 MB size max and 3 rotated files
        auto max_size = 1048576 * 5;
        auto max_files = 3;
        auto logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", max_size, max_files);
    }
    

    // Create a daily logger - a new file is created every day at 2:30 am

    auto logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
    
    

    Since Q&A does not provide support on how to use third-party libraries, for questions about these libraries, you can seek help on GitHub or StackOverflow.

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.