A simple C++ logging setup that builds on spdlog to provide easily configurable console and file outputs, along with convenient logging macros.
- Console and File output sinks.
- Configurable log levels, file sizes, and rotation behavior.
- Log macros (
LOG_INFO,LOG_ERROR, etc.) withfmtformatting. - Simple CMake build and example usage.
- JSON configuration initialization: Easily set up logger sinks via a JSON config file
You can include this Logger project as a subdirectory in your own CMake project. For example, if you clone or include it as a git submodule under submodules/Logger, you can do:
add_subdirectory(submodules/Logger)Next, link the Logger library to your target. For instance:
target_link_libraries(MyApp PRIVATE Logger)Because the Logger uses a PUBLIC include for its headers, you do not need extra include_directories for the Logger — they are automatically made available to your target.
You can manually add sinks using the provided builder methods:
#include "Logger.h"
int main()
{
// Setup file output with custom options
logging::addFileOutput()
.setFilename("TestLog.log")
.setLevel(LogLevel::Info)
.setMaxFileSize(20_MB)
.setRotateOnSession(true);
// Setup console output with custom level
logging::addConsoleOutput().setLevel(LogLevel::Debug);
LOG_INFO("Hello logger, the answer is: {}", 42);
return 0;
}Alternatively, you can initialize the logger via a JSON configuration file. Create a JSON file (e.g. logger_config.json) with the following structure:
{
"sinks": [
{
"type": "console",
"level": "debug",
"max_skip_duration": 0,
"pattern": "[%Y-%m-%d %H:%M:%S.%e] [%l] %v"
},
{
"type": "file",
"level": "info",
"max_skip_duration": 0,
"file_name": "logs/app.log",
"max_file_size": "10_MB",
"max_files": 3,
"rotate_on_session": true
}
]
}Then in your code initialize the logger using:
#include "Logger.h"
int main()
{
// Initialize logger with JSON configuration.
// Missing settings will be replaced with default values.
logging::initializeLogger("logger_config.json");
LOG_INFO("Logger initialized using JSON configuration!");
return 0;
}
This approach leverages nlohmann json (integrated via CPM) to parse the configuration file and set up the sinks accordingly. If a key is missing, a sensible default will be used.
This project defines the JSON key names (used in the configuration file) as CMake Cache variables. This means that when you include Logger as a subdirectory in your project, you can easily override these defaults without modifying the Logger source code.
Before adding the Logger subdirectory in your parent project's CMakeLists.txt, set the desired values for the JSON keys. For example:
# Override default JSON key names
set(LOGGER_CONFIG_SINK "custom_sinks" CACHE STRING "JSON key for logger sinks")
set(LOGGER_CONFIG_LEVEL "custom_level" CACHE STRING "JSON key for log level")
# ...set other keys as needed
add_subdirectory(path/to/Logger)Alternatively, you can override these values on the command line when configuring your project:
cmake -DLOGGER_CONFIG_SINK="custom_sinks" -DLOGGER_CONFIG_LEVEL="custom_level" <path-to-your-project>If not otherwise specified, the logger will provide default values:
- Log Level :
info - Max Skip Duration :
0(microseconds) - Pattern :
[%Y-%m-%d %H:%M:%S.%e] [%l] %v
- Log Level :
info - Max Skip Duration :
0(microseconds) - File Name :
default.log - Max File Size :
10 MB - Max Files :
3 - Rotate on session :
false
- Log Level :
info - Max Skip Duration :
0(microseconds) - Check For Debugger :
false
- Update
CMakeLists.txtorbuild.pyaccording to your needs. - Requires C++20 and CMake ≥ 3.25.
- By default, logs are written at Info level to the configured sinks.
- The spdlog library is included via CPM in the
libsdirectory.
Enjoy streamlined logging with SPDLog!