Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-3052

Make QMessageLogger available in Python

XMLWordPrintable

    • Icon: Suggestion Suggestion
    • Resolution: Unresolved
    • Icon: P3: Somewhat important P3: Somewhat important
    • None
    • 6.8.2
    • PySide
    • None
    • Linux/Wayland, Windows
    • 48a2c4e67 (dev), 6f5d32b26 (6.9), ce74d3378 (6.8)

      Qt provides the QMessageLogger class to explicitly set the source location for a given logging call. This class is not available as a Python binding.
      My usecase would be to connect an existing logging setup utilizing Pythons builtin logging module to the Qt logging system.
      For this I am installing a custom logging handler that would then extract the source location from the logging.LogRecord object and override the Qt logging source location.
      However, without the availability of QMessageLogger in PySide only the regular qDebug etc. functions are available which report an unknown source location.

      AFAIK there is also no other way to set the source location when logging.

      I also tried getting the current logging message handler using qInstallMessageHandler, however, the function always seems to return None instead of the current handler.
      I would have then called this handler explicitly in the callback provided to pythons logging system.

      Maybe it would be possible to make QMessageLogger available in PySide.
      Thank you!

      QMessageLogger example

      import logging
      
      from PySide6.QtCore import QMessageLogger # NameError: name 'QMessageLogger' is not defined
      
      class LogHandler(logging.Handler):
      
          def emit(self, record: logging.LogRecord):
              logger = QMessageLogger(record.filename, record.lineno, record.funcName)
              match record.levelno:
                  case logging.WARNING:
                      logger.warning(record.message)
      
      
      logging.basicConfig(handlers=[LogHandler()])
      
      logging.warning("Test")
      

      qInstallMessageHandler example:

      from PySide6.QtCore import qInstallMessageHandler
      
      def test(*pos):
          print(pos)
      
      handler = qInstallMessageHandler(test)
      
      print(type(handler)) # <class 'NoneType'>
      
      

        For Gerrit Dashboard: PYSIDE-3052
        # Subject Branch Project Status CR V

            shpremna Shyamnath Premnadh
            johan-qt Johan B
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                There is 1 open Gerrit change