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

howto: redirect console output to syslog



  • I have a QtQuick/QML application running on a remote embedded target system. I have syslog configured on the target to direct log messages to a log server.

    Now, I'd like to have the standard out and err console output also redirected to the local syslog so I can get all of my application feedback in one place.

    Is there a "best practices" way to do this? Or will I want/need to obtain all this output within my application and log it through "normal channels"?



  • The solution that works for me (your mileage may vary) is to call qInstallMessageHandler() from the Qt Core package and supply my own QtMessageHandler function. There's a nice example of this in the help docs.

    https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler


  • Lifetime Qt Champion

    @alpartis said in howto: redirect console output to syslog:

    Is there a "best practices" way to do this?

    Don't print debug and error message to stdout/stderr but use a proper logging system. Printing somthing to stdout/err is for sure nothing to be considered except basic debugging or a command line program.



  • Also it may not be a good idea to send the standard out and err console output of your app to syslog because it can be a lot. And you may not want to mess up with OS output. It may be better to send them to a file. You can use Qt logging system to replace std output.
    https://doc.qt.io/qt-5/qloggingcategory.html



  • @Christian-Ehrlicher I am mostly concerned with messages that Qt prints to stderr. In my target execution environment, the console output is lost, so it would be helpful to get the various warnings and errors that the Qt library code generates (especially with regard to QML errors that are not encountered until runtime) redirected to the system logger.

    I'm aware that I can do this by redirecting stdout and stderr when my application is launched. My question is whether there is a better way to do this from within the application itself.



  • @JoeCFD: Whether, or not, it's a good idea, there is very helpful information that the Qt library code prints to the console (stderr for the most part) and in my case, that information will be lost if it isn't redirected to a persistent destination. It's not a matter of my use of a Qt logging API, but the Qt library's use of such things. When the Qt library points out that it's not displaying one of my QML components because it cannot find one of my sub-components, it does so through stderr -- I just want to know the best way to get that information to the logger where I can see and record it.



  • The solution that works for me (your mileage may vary) is to call qInstallMessageHandler() from the Qt Core package and supply my own QtMessageHandler function. There's a nice example of this in the help docs.

    https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler


Log in to reply