How can I make a threadsafe logging handler subclass that triggers events in the main Qt loop?

I have a PySide6 application that uses threading and logging. My logger has a custom handler defined below, which pops up a messagebox that requires user attention for higher level logging events:

import logging
from PySide6.QtWidgets import QMessageBox

class QtHandler(logging.Handler):
    def emit(self, record):
        if record.levelno >= logging.ERROR:
            QMessageBox.critical(None, "Error", self.format(record))
        elif record.levelno >= logging.WARNING:
            QMessageBox.warning(None, "Warning", self.format(record))

This works fine for single-threaded operations, but fails when a thread needs to log something that would trigger the messagebox, since that occurs in the main loop and it is not safe to trigger main loop events from threads.

My first attempt to fix this was to try to make the popup box appear only through emission of a Signal mediated by Qt.QueuedConnection, like so:

import logging
from PySide6.QtCore import QObject, Signal, Slot, Qt
from PySide6.QtWidgets import QMessageBox

class QtHandler(logging.Handler, QObject):
    queue_messagebox = Signal(object)
    
    def __init__(self, parent=None):
        # Initialize both QObject and logging.Handler
        QObject.__init__(self, parent)
        logging.Handler.__init__(self)
        # Connect the signal to the slot using a queued connection to ensure thread safety
        self.queue_messagebox.connect(self.show_message_box, Qt.QueuedConnection)

    def emit(self, record):
        # This is where the logging record is processed
        self.queue_messagebox.emit(record)  # Emit the signal with the log record

    @Slot(object)
    def show_message_box(self, record):
        # Create the message box based on the log level
        if record.levelno >= logging.ERROR:
            QMessageBox.critical(None, "Error", self.format(record))
        elif record.levelno >= logging.WARNING:
            QMessageBox.warning(None, "Warning", self.format(record))

But this fails with the statement that my emit() function is getting too many arguments. My assumption is that this is due to the fact that both logging.Handler and QObject have methods called emit and I am overriding one of them, when I need both of them to work.

How can I address this conflict, or is there another way I could go about making this logging handler threadsafe?

6

For posterity, this worked in the end, thank you @mahkitah for the suggested framework:

import logging
from PySide6.QtCore import QObject, Signal, Slot, Qt
from PySide6.QtWidgets import QMessageBox

#QObject designed solely to emit signals
class MessageBoxEmitter(QObject):
    emit_message = Signal(object)

class QtHandler(logging.Handler):
    def __init__(self, parent=None):
        super().__init__()
        # Create an instance of the internal QObject to handle signal emissions
        self.emitter = MessageBoxEmitter()
        # Connect the internal signal to the message box displaying slot - queued connection to ensure thread safety
        self.emitter.emit_message.connect(self.show_message_box, Qt.QueuedConnection)

    def emit(self, record):
        # Emit the signal with the log record
        self.emitter.emit_message.emit(record)

    @Slot(object)
    def show_message_box(self, record):
        # Create the message box based on the log level
        if record.levelno >= logging.ERROR:
            QMessageBox.critical(None, "Error", self.format(record))
        elif record.levelno >= logging.WARNING:
            QMessageBox.warning(None, "Warning", self.format(record))

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật