What it is
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.
plog uses macros for logging at different severity levels and supports multiple appenders (console, file, etc.).
Installation
add_subdirectory(plog)Getting started
The smallest useful thing you can do with it, and what each part means.
#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;
}Advanced usage
Where the library earns its place over a simpler alternative.
plog::init(plog::debug, "log.txt").addAppender(new plog::ConsoleAppender<plog::TxtFormatter>());
PLOGI << "Log to both console and file";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";plog::init(plog::debug, "log.txt", 1000, 5);
PLOGI << "Logs will roll over every 1000 lines, keeping 5 backups.";Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Logs not written to file
- Check file path permissions and ensure `plog::init` is called before logging.
- Too much log output
- Adjust the severity threshold passed to `plog::init` (e.g., `plog::info`).
- Thread-safety issues
- plog is thread-safe, but custom appenders must ensure safe writes.
Best practices
- 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).
Background
Why it exists, and what it was reacting to.
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.
