Language: CPP
Logging
plog was created to provide a lightweight and portable alternative to heavier C++ logging libraries like log4cxx and glog. It is header-only, easy to integrate, and flexible enough for both small applications and large systems. Its modular design allows developers to plug in custom appenders and formatters.
plog is a portable, simple, and extensible C++ logging library. It supports multiple logging destinations, severity levels, thread safety, and works across platforms with minimal dependencies.
add_subdirectory(plog)vcpkg install plogconan install plogDownload plog headers and include them in your projectplog uses macros for logging at different severity levels and supports multiple appenders (console, file, etc.).
#include <plog/Log.h>
#include <plog/Initializers/RollingFileInitializer.h>
int main() {
plog::init(plog::debug, "log.txt");
PLOGD << "This is a debug message";
PLOGI << "This is an info message";
PLOGW << "This is a warning";
PLOGE << "This is an error";
PLOGF << "This is fatal";
return 0;
}Initializes plog with rolling file logs and writes messages at different severity levels.
plog::init(plog::debug, "log.txt").addAppender(new plog::ConsoleAppender<plog::TxtFormatter>());
PLOGI << "Log to both console and file";Logs messages to both file and console simultaneously.
struct MyFormatter {
static std::ostream& header(std::ostream& stream) { return stream; }
static std::ostream& format(std::ostream& stream, const plog::Record& record) {
return stream << record.getMessage() << " (custom)\n";
}
};
plog::init(plog::debug, "log.txt", true, 1000, 3).addAppender(new plog::ConsoleAppender<MyFormatter>());
PLOGI << "Custom formatted log";Demonstrates creating a custom log formatter.
plog::init(plog::debug, "log.txt", 1000, 5);
PLOGI << "Logs will roll over every 1000 lines, keeping 5 backups.";Configures rolling logs with file size and count limits.
Initialize logging early in the main function before other components run.
Use different severity levels consistently to filter logs easily.
Combine file and console appenders to keep persistent and real-time logs.
Use rolling file logging to avoid uncontrolled log growth.
Implement custom formatters if you need structured logs (e.g., JSON).