A lightweight, decoupled logging framework for C++ applications that follows the Strategy and Factory design patterns. This system allows for flexible log formatting and multiple output destinations through a simple interface.
- Multi-Level Logging: Supports four distinct log levels:
DEBUG,INFO,ERROR, andFATAL. - Flexible Output Targets:
- Console Output: Standard output logging via
LogConsoleOutput. - File Output: Persistent logging to disk via
LogFileOutputwith configurable file paths.
- Console Output: Standard output logging via
- Pluggable Formatting: Uses a dedicated
LogFormatinterface to transform raw log data into structured strings. - Configuration Driven: Dynamically switches log types (console/file), file names, and priority levels using an external
.conffile. - Macro-Based API: Provides easy-to-use macros that automatically capture the calling file name and line number.
- Memory Management: The
Loggerclass handles the lifecycle of its delegate formatters and outputters.
The system is built on a composition-based design:
- Logger: The core engine that manages log levels and delegates work to the formatter and outputter.
- LogFormat (Interface): Defines how the log message, level, file, and line number are stringified.
- LogOutput (Interface): Defines where the formatted string is sent.
- LogFac (Factory): A singleton class that manages the global logger instance and handles initialization from configuration files.
Define your settings in a simple key-value format:
log_type=file
log_file=log.txt
log_level=info
#include "log_fac.h"
int main() {
// Initialize with config file
LogFac::Instance().init("log.conf");
// Log messages
LOGINFO("Application started.");
LOGDEBUG("This will only show if log_level is debug.");
LOGERROR("An unexpected error occurred.");
return 0;
}XML Formatting Support: Implementation of an XMLLogFormat class to allow logs to be parsed easily by automated tools.
Asynchronous Logging: Adding a thread-safe queue to prevent logging operations from blocking the main application thread.
Log Rotation: Support for rolling log files based on file size or time intervals.
Colored Console Output: Enhancing the LogConsoleOutput to support ANSI color codes for different log levels.