Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt Thread-Safe Logging



  • Hi,

    I am currently working on a Qt project and I would like to use a logging mechanism for debugging and monitoring purposes.

    But after searching around for some answers in older posts in Qt Forums and Stack Overflow, I am a bit confused if I should use the buildin logging functions qDebug, qInfo and etc or I should use a third-party library. You can find below the links with some active Qt logging libraries:

    https://github.com/dept2/CuteLogger
    https://github.com/MEONMedical/Log4Qt
    https://github.com/francescmm/QLogger
    https://github.com/sfadiga/QLogger

    The program that I am currently working it's multithreaded, so the logging mechanism should be thread-safe, log async (if possible) and I would like to redirect our logs in the Linux syslog system. Also, I saw that CuteLogger is using a Singleton pattern and the global instance it's visible across all the project and you can easily use the log functions by accessing the global instance which it's considered as a plus if it can be available from any other library or buildin mechanism.

    So, which approach from listed below is the best fit for my case in your opinion? Any alternative solution or share your ideas with pros and cons for each approach?

    1. Use Qt buildin qInfo, qDebug and etc by using a custom message handler that redirects logging to syslog?
    2. Use a third-party library like CuteLogger and if it's supported to redirect logs in the Linux syslog?

    Thanks.


  • Lifetime Qt Champion

    Hi @nsourl,

    I'd go with Qt's categorized logging (i.e. qCDebug, see here as a starting point).

    Logging to syslog should be possible, if I read this blog entry.

    Regards



  • Hi @aha_1980 ,

    Thank you for your prompt reply and your help on my confusion.

    So, you suggest to use Qt's categorized logging and then use a message handler (I suppose by using QMutexLocker for multithreading) as described in the blog to redirect the logs to syslog. Is that right?

    It seems that now at least I have a clean and good approach for logging in my project.

    Although, has anyone from the Qt community tried or used the third-party libraries I mentioned above? Is there a specific reason that I should use on of them instead of proceeding with @aha_1980 approach? Any additional ideas, comments or suggestions are welcome too.

    Thanks.


  • Lifetime Qt Champion

    I'm using Log4Qt since years without any real problems.


  • Lifetime Qt Champion

    @nsourl said in Qt Thread-Safe Logging:

    So, you suggest to use Qt's categorized logging and then use a message handler (I suppose by using QMutexLocker for multithreading) as described in the blog to redirect the logs to syslog. Is that right?

    No, as Qt already has integrated syslog capabilities. Please read my second link.

    Regards


Log in to reply