A modern, lightweight, thread-safe logging library in C++17 with:
- β
Log level filtering (
INFO,WARNING,ERROR) - β Colored console output
- β Log file rotation based on file size
- β Customizable log file name
- β Graceful shutdown with background worker thread
- β Designed for multithreaded C++ applications
[2025-07-06 12:45:29] [INFO] Thread 1: Startup (white) [2025-07-06 12:45:29] [WARNING] Disk space low (yellow) [2025-07-06 12:45:29] [ERROR] Thread 1: Failure (red)
| Feature | Description |
|---|---|
| π§΅ Thread-safe logging | Safe from race conditions using mutex + CV |
| π₯ Colored console output | Uses ANSI escape codes for INFO, WARN, ERR |
| πͺ΅ Log level filtering | Set a minimum level to control verbosity |
| πΎ File logging | All logs saved to a file (logs.txt) |
| π Log file rotation | Rotates logs automatically if size > 5MB |
| π§ Custom output file name | Configure via logger.init() |
| β Graceful shutdown | Prevents logging after destruction |
- Windows with
g++(MinGW or MSYS2) - C++17 or higher
g++ -std=c++17 -o logger_app main.cpp src/Logger.cpp
βΆοΈ Run
.\logger_app.exe
π Project Structure
ThreadSafeLogger/
βββ src/
β βββ Logger.cpp
β βββ Logger.h
βββ main.cpp
βββ .gitignore
βββ README.md
π Example Usage
Logger& logger = Logger::getInstance();
logger.init("app_logs.txt", LogLevel::INFO);
logger.log("App started", LogLevel::INFO);
logger.log("Low disk warning", LogLevel::WARNING);
logger.log("Fatal crash", LogLevel::ERROR);
logger.shutdown();
π€ Author
Darelene
github.com/Chanchal70021a