If we change the logging level, then we can change the information sent to the log. For example, if we want more information: In this case, the commands don’t print anything to the console, since nothing at WARNING level or above is logged by them. A question that comes up from time to time is about how to log to a GUI application. The Qt framework is a popular cross-platform UI framework with Python bindings using PySide2 or PyQt5 libraries. The following example shows how to log to a Qt GUI. This introduces a simple $_{QtHandler}$class which takes a callable, which should be a slot in the main thread that does GUI updates. A worker thread is also created to show how you can log to the GUI from both the UI itself (via a button for manual logging) as well as a worker thread doing work in the background (here, just logging messages at random levels with random short delays in between). The worker thread is implemented using Qt’s QThread class rather than the threading module, as there are circumstances where one has to use$_{QThread}$ , which offers better integration with other$_{Qt}$ components. The code should work with recent releases of either$_{PySide2}$ or$_{PyQt5}$ . You should be able to adapt the approach to earlier versions of Qt. Please refer to the comments in the code snippet for more detailed information. (continues on next page) 43 import datetime import logging import random import sys import time # Deal with minor differences between PySide2 and PyQt5 try : from PySide2 import QtCore, QtGui, QtWidgets Signal =QtCore .Signal Slot =QtCore .Slot except ImportError: from PyQt5import QtCore, QtGui, QtWidgets Signal =QtCore .pyqtSignal Slot =QtCore .pyqtSlot logger=logging.getLogger(__name__ ) # $ python app.py --log-level DEBUG start foo DEBUG start About to start foo INFO start Started the 'foo' service. $ python app.py --log-level DEBUG stop foo bar DEBUG stop About to stop 'foo' and 'bar' INFO stop Stopped the 'foo' and 'bar' services. $ python app.py --log-level DEBUG restart foo bar baz DEBUG restart About to restart 'foo', 'bar' and 'baz' INFO restart Restarted the 'foo', 'bar' and 'baz' services. $ python app.py --log-level WARNING start foo $ python app.py --log-level WARNING stop foo bar $ python app.py --log-level WARNING restart foo bar baz And if we want less: 29 A Qt GUI for logging