Apache Log4cxx  Version 1.3.0
Loading...
Searching...
No Matches
Qt Support

When using Qt, messages from the Qt framework itself or other libraries may use the QDebug classes. By default, this will print to stderr, thus bypassing the logger entirely. In order to have these messages routed to Log4cxx, a message handler for Qt must be installed.

Log4cxx provides a cmake build option LOG4CXX_QT_SUPPORT=ON which adds the log4cxx::qt namespace methods for directing Qt messages to Log4cxx and using the Qt event loop to process a configuration file change. Use the target log4cxx-qt instead of log4cxx in your target_link_libraries cmake directive. Also, including log4cxx-qt/logger.h allows you to use QString values in the LOG4CXX_WARN, LOG4CXX_INFO, LOG4CXX_DEBUG etc. macros.

To install a message handler that will route the Qt logging messages through Log4cxx, include the messagehandler.h and call qInstallMessageHandler as follows:

...
qInstallMessageHandler( log4cxx::qt::messageHandler );
LOG4CXX_EXPORT void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message)
The messageHandler function is a log4cxx replacement of the standard Qt message handler.

Note that by default, this message handler also calls abort upon a fatal message.

For how to use the Qt event loop to monitor the configuration file, see the com/foo/config-qt.h and com/foo/config-qt.cpp example files.

Note that when using the above technique you must configure Log4cxx after creating your QCoreApplication instance (see the MyApp-qt.cpp file for an example of this).