qtlogger

Project Status C++ Standard Qt Version License

Qt Logger - Advanced Qt logging library

A simple yet powerful logging solution for the Qt Framework. This project is designed to provide developers with an intuitive and configurable logging system for Qt-based applications. QtLogger features a customizable pipeline for processing log messages through the qInstallMessageHandler() function, allowing for flexible handling and output of logs.

Pretty output

Quick start

Just copy qtlogger.h to your project directory and use the global gQtLogger object for configuration:

#include "qtlogger.h"

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    
    gQtLogger.configure();  // That's it!
    
    qDebug() << "It just works!";
    
    return app.exec();
}

Key Features

Integration

There are multiple ways to integrate QtLogger into your project:

Option 1: Header-Only (Simplest)

Just copy qtlogger.h to your project and include it:

#include "qtlogger.h"

This is the simplest approach - no build configuration needed.

Option 2: CMake

Link against the QtLogger library in your CMakeLists.txt:

# Add QtLogger subdirectory
add_subdirectory(path/to/qtlogger)

# Link your target
target_link_libraries(your_target PRIVATE qtlogger)

Option 3: qmake

Include the QtLogger .pri file in your .pro file:

# In your project's .pro file
include(path/to/qtlogger/qtlogger_link.pri)

More examples

Formatted logging with file rotation

This example demonstrates how to set up logging with human-readable colored output to the console (stderr) and automatic file rotation. The configure() method creates a rotating log file that compresses old logs when they exceed the specified size limit.

#include "qtlogger.h"

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    
    gQtLogger.configure("app.log", 1024 * 1024, 5,   // Max 5 files of 1 MB each
                        QtLogger::RotatingFileSink::Compression);
    
    qDebug() << "It just works!";
    
    return app.exec();
}

Advanced configuration

For more complex scenarios, you can create multiple pipelines with different filters, formatters, and sinks. Each pipeline processes log messages independently, allowing you to route different types of logs to different destinations with specific formatting.

#include "qtlogger.h"

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    // Configure with multiple pipelines
    gQtLogger
        .moveToOwnThread()                 // Asynchronous logging
        .addSeqNumber()
        .pipeline()
            .filterLevel(QtWarningMsg)     // Only warnings and above
            .filterDuplicate()
            .formatPretty()
            .sendToStdErr(true)            // Colored output
        .end()
        .pipeline()
            .format("%{time} [%{category}] %{type}: %{message}")
            .sendToFile("app.log", 1024 * 1024, 5,  // Rotating logs (1MB, 5 files)
                        QtLogger::RotatingFileSink::RotationOnStartup
                        | QtLogger::RotatingFileSink::RotationDaily
                        | QtLogger::RotatingFileSink::Compression)
        .end()
        .pipeline()
            .addAppInfo()
            .addHostInfo()
            .filter("^(?!.*password:).*$")  // Filter out sensitive data via Regex
            .formatToJson()
            .sendToHttp(QUrl("https://logs.example.com"))
        .end();

    // In advanced configuration mode, you must call this method explicitly
    gQtLogger.installMessageHandler();

    qDebug() << "Application started";
    qWarning() << "This is a warning";

    return app.exec();
}

Sentry Integration

Send warnings and errors to Sentry for error tracking. QtLogger provides a built-in SentryFormatter and utility functions for easy integration.

#include "qtlogger.h"

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    app.setApplicationName("MyApp");
    app.setApplicationVersion("1.0.0");

    // Check required environment variables
    // Set SENTRY_DSN or (SENTRY_HOST + SENTRY_PROJECT_ID + SENTRY_PUBLIC_KEY)
    if (!QtLogger::checkSentryEnv()) {
        qWarning() << "Sentry environment variables not set";
        return 1;
    }

    gQtLogger
        .moveToOwnThread()  // Async logging for non-blocking HTTP requests

        // Console output for local debugging
        .pipeline()
            .formatPretty(true)
            .sendToStdErr()
        .end()

        // Send warnings and errors to Sentry
        .pipeline()
            .addAppInfo()               // Add app name, version
            .addSysInfo()               // Add OS, kernel, CPU info
            .addHostInfo()              // Add hostname
            .filterLevel(QtWarningMsg)  // Only warnings and above
            .filterDuplicate()          // Prevent spam
            .formatToSentry()
            .sendToHttp(QtLogger::sentryUrl(), QtLogger::sentryHeaders())
        .end();

    gQtLogger.installMessageHandler();

    qWarning() << "This warning is sent to Sentry";
    qCritical() << "This error is sent to Sentry";

    return app.exec();
}

Environment variables:

Configuration from file

You can configure the logger at runtime using an INI configuration file. This approach allows you to change logging behavior without recompiling your application.

// Load configuration from INI file
gQtLogger.configureFromIniFile("config.ini");

Example config.ini:

[logger]
filter_rules = "*.debug=false"
message_pattern = "%{time} [%{category}] %{type}: %{message}"
stdout_color = true
path = "app.log"
max_file_size = 1048576
max_file_count = 5
rotate_on_startup = true
rotate_daily = false
compress_old_files = false
async = true

Comparison with Other Libraries

Feature QtLogger Log4Qt QsLog spdlog
Qt Integration Native (qDebug(), qInfo(), etc.) Own API Own API Own API
Header-only option
Zero code changes
Colored console output
File rotation ✅ (size/daily/startup) ✅ (size) ✅ (size) ✅ (size/daily)
Gzip compression
Async logging
JSON formatter
Sentry formatter
Pattern formatter
HTTP sink
Android logcat
macOS/iOS os_log
Linux syslog/journald
Duplicate suppression
Category filtering
Regex filtering
Fluent API
INI configuration ✅ (XML)
Qt dependency Required Required Required None
License MIT Apache 2.0 BSD/MIT MIT

Requirements

Documentation

Full documentation is available in the docs/ directory:

Examples are available in the examples/ directory.

Versioning

This project adheres to Semantic Versioning 2.0.0 (SemVer).

Semantic Versioning is a versioning scheme that uses a three-part version number: MAJOR.MINOR.PATCH, where:

For more details, refer to the Semantic Versioning specification.

By following SemVer, we aim to provide clear and predictable version updates for our users.

License

This project is licensed under the MIT License. See the LICENSE file for details.