How can I redirect the logger to a wxPython textCtrl using a custom logging handler?

后端 未结 3 709
灰色年华
灰色年华 2020-12-28 20:53

I\'m using a module in my python app that writes a lot a of messages using the logging module. Initially I was using this in a console application and it was pretty easy to

3条回答
  •  青春惊慌失措
    2020-12-28 21:49

    You will need to create a custom logging.Handler and add it to your logging.Logger.

    From the documentation:

    Handler objects are responsible for dispatching the appropriate log messages (based on the log messages’ severity) to the handler’s specified destination. Logger objects can add zero or more handler objects to themselves with an addHandler() method. As an example scenario, an application may want to send all log messages to a log file, all log messages of error or higher to stdout, and all messages of critical to an email address. This scenario requires three individual handlers where each handler is responsible for sending messages of a specific severity to a specific location.

    See http://docs.python.org/library/logging.html#handler-objects for the Handler API.

    In particular, it is the Handler.emit(record) method that you can implement to specify the destination of the output. Presumably, you would implement this to call TextCtrl.AppendText.

提交回复
热议问题