Skip to main content

Raymii.org Raymii.org Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | RSS Feed

Disable (debug) logging in QT and QML

Published: 27-02-2021 | Author: Remy van Elst | Text only version of this article


❗ This post is over three years old. It may no longer be up to date. Opinions may have changed.


In QT you can use a few functions from the qDebug.h header like qDebug() and qWarning() to log information to the console. In QML you can use the likes of console.log(), console.error(). It's also very easy to implement your own logger (eg. SyslogMessageHandler) if you want something different, like logging to syslog and the console. In this post I'll show you how to disable both forms of logging in a release build, qml and qt have different ways to manage their output.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!

Information online mostly suggested to define QT_NO_DEBUG_OUTPUT, but that failed to disable QML logging. Not sure if that's because my sample project had it's own MessageHandler, but in the end I figured out how to disable everything.

The QT documentation pages on debugging and qml logging are helpful, but as most often with their documentation, it's so comprehensive that it is not easy to find how to do just one specific thing, in this case, just disabling logging in a release build.

Disable QT logging

In your .pro file add the following lines:

# Suppress logging output for release build.
CONFIG(release, debug|release): DEFINES += QT_NO_DEBUG_OUTPUT
CONFIG(release, debug|release): DEFINES += QT_NO_INFO_OUTPUT
CONFIG(release, debug|release): DEFINES += QT_NO_WARNING_OUTPUT

Each flag disables the appropriate qLog() method, for example, QT_NO_DEBUG_OUTPUT disables qDebug().

After editing your .pro file, remember to clean and rebuild your project.

Why not just CONFIG(release):? If you have multiple options, this only triggers if the last option is release.

CONFIG(release, debug|release) evaluates to true if CONFIG contains release but not debug, or if it contains both debug and release but debug doesn't appear after the last occurrence of release. For example:

CONFIG += release debug release

Because the last debug comes after the last debug, CONFIG(release, debug|release) is true.

Disable QML logging

In main.cpp include the QLoggingCategory header:

#include <QLoggingCategory>

Before your other logging handlers (like qInstallMessageHandler()), add this piece of code:

#ifdef QT_NO_DEBUG
    QLoggingCategory::setFilterRules("*.debug=false\n"
                                     "*.info=false\n"
                                     "*.warning=false\n"
                                     "*.critical=true");
    fprintf(stderr, "Disabling QML logging in release build.\n");
#else
    fprintf(stderr, "QML logging enabled.\n");
#endif

QT_NO_DEBUG is exported automatically by QT when compiling a release build. Filter rules allow for more control over what is logged when and where, but this code just disables everything except critical. It must be newline separated (\n in the method parameters), comma's do not work.

The fprintf line is optional but does help to know what's going on and when there is logging and when logging is disabled.

I tried using an #if defined(QT_NO_DEBUG_OUTPUT) check for more granular control, that failed to work however. Every compile (debug or release) disabled the logging, not sure why and didn't dig in too much either.

The other way is with an environment variable. Here they suggest the following flag:

QT_LOGGING_RULES=qml=false

This failed to work for me, both exported on the command line or in main.cpp as below:

#ifdef QT_NO_DEBUG
   qputenv("QT_LOGGING_RULES", "qml=false");
#endif

Your mileage may vary. If it does work for you, please let me know.

Tags: articles , c++ , cpp , debugging , development , gui , logging , qml , qt , qt5