A lightweight, high-performance, thread-friendly logging library for C++ featuring stream-style macros, automatic log rotation, configurable log levels, file logging, console logging, and internal cache support.
This project uses my reusable utility library cpp-utils for common internal components such as thread-safe queueing and shared utilities.
๐ https://github.com/jayasankar-jp/cpp-utils
git clone git@github.com:jayasankar-jp/logger-cpp.git
cd logger-cppThis project requires cpp-utils.
git clone https://github.com/jayasankar-jp/cpp-utils.git
cd cpp-utils
mkdir build
cd build
cmake ..
make
sudo make installNavigate to your project directory.
git clone git@github.com:jayasankar-jp/logger-cpp.git
cd logger-cpp
mkdir build
cd build
cmake ..
makesudo make install| Type | Path |
|---|---|
| Library | /usr/local/lib/libLogger.a |
| Headers | /usr/local/include/ |
| CMake Config | /usr/local/lib/cmake/Logger/ |
#include <Logger.h>
int main()
{
Logger::getInstance();
Logger::setAppName("MQTT_APP");
Logger::setLogPath("./LOGS");
Logger::setLogLevel(127);
log_info << "Application Started";
log_error << "Connection Failed";
log_warn << "Retrying";
log_debug << "Debug Data = " << 10;
log_critical << "Critical Error";
return 0;
}#include <Logger.h>
#include <thread>
#include <chrono>
int main()
{
Logger::getInstance();
Logger::setAppName("MY_TEST_APP");
Logger::setLogLevel(127);
Logger::setMaxFileSizeMB(50);
Logger::setMaxFileGenPeriodMin(1);
std::thread([]{
while(true)
{
log_info << "Thread 1 Running";
log_debug << "Thread 1 Debug";
}
}).detach();
std::thread([]{
while(true)
{
log_warn << "Thread 2 Warning";
log_critical << "Thread 2 Critical";
}
}).detach();
while(true)
std::this_thread::sleep_for(std::chrono::seconds(10));
}#define log_error LogStream(__FILE__, __LINE__, LogLevel::Error)
#define log_info LogStream(__FILE__, __LINE__, LogLevel::Info)
#define log_critical LogStream(__FILE__, __LINE__, LogLevel::Critical)
#define log_verbose LogStream(__FILE__, __LINE__, LogLevel::Verbose)
#define log_warn LogStream(__FILE__, __LINE__, LogLevel::Warn)
#define log_debug LogStream(__FILE__, __LINE__, LogLevel::Debug)Each log automatically includes:
- Timestamp
- File Name
- Line Number
- Log Level
- Message
enum class LogLevel
{
Error = 1 << 0,
Info = 1 << 1,
Critical = 1 << 2,
Verbose = 1 << 3,
Warn = 1 << 4,
Debug = 1 << 5,
Console = 1 << 6
};| Level | Value |
|---|---|
| Error | 1 |
| Info | 2 |
| Critical | 4 |
| Verbose | 8 |
| Warn | 16 |
| Debug | 32 |
| Console | 64 |
| Value | Enabled Logs |
|---|---|
| 1 | Error |
| 3 | Error + Info |
| 31 | Error + Info + Critical + Verbose + Warn |
| 63 | All File Logs |
| 127 | All Logs + Console Output |
Logger::setLogLevel(127);<LOG_PATH>/<APP_NAME>_<DATE>_<TIME>.log
./LOGS/MY_TEST_APP_27-04-2026_13:40:12.log
A new log file is created automatically when:
- ๐ฆ File size exceeds limit
- โฑ๏ธ Rotation time reached
Logger::setAppName("MY_APP");Logger::setLogPath("./LOGS");Logger::setLogLevel(127);Logger::setMaxFileSizeMB(50);Logger::setMaxFileGenPeriodMin(10);Logger::desableCash();Description:
- Disables internal cache/buffer
- Writes logs more directly to file
- Useful for debugging
- May reduce performance under high logging load
- โก High-speed logging
- ๐งต Thread-friendly
- ๐ File logging
- ๐ฅ๏ธ Console logging
- ๐ File rotation
- ๐ File & line info
- ๐ง Bitmask level control
- ๐งฑ Stream-style syntax (
<<) - ๐ Easy CMake integration
- ๐ง Singleton architecture
- ๐พ Internal cache support
Logger is built using reusable modules from cpp-utils, helping reduce duplicated code and improve maintainability.
Used components may include:
- Thread-safe Queue
- Common utility helpers
- Shared infrastructure modules
๐ https://github.com/jayasankar-jp/cpp-utils
find_package(Logger REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app Logger::Logger)list(APPEND CMAKE_PREFIX_PATH "/your/install/path")
find_package(Logger REQUIRED)add_subdirectory(logger-cpp)
add_executable(app main.cpp)
target_link_libraries(app Logger)- Include:
#include <Logger.h>- Ensure log directory exists
- Static library build by default
- Use
127for all logging
- Async lock-free queue
- Shared library (
.so) - JSON structured logs
- Per-module filtering
- Colored console logs
- Cache tuning APIs
Jayasankar JP
Pull requests are welcome.
If you find bugs or want new features, open an issue in the repository.
Some internal modules come from cpp-utils.
If making core utility changes, please also review:
๐ https://github.com/jayasankar-jp/cpp-utils
If you find bugs or want new features, open an issue on GitHub.