plog

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.

Installation

cmake: add_subdirectory(plog)
vcpkg: vcpkg install plog
conan: conan install plog
manual: Download plog headers and include them in your project

Usage

plog uses macros for logging at different severity levels and supports multiple appenders (console, file, etc.).

Initialize and log

#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.

Multiple appenders

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.

Custom formatter

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.

Rolling file logging

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.

Error Handling

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).