wxPython and logging

Greetings.
I have only take seriously to learn wxPython to make my application for DB and already totally loosing any motivation.
I am going through every step, from really really basic and toward more complex. For example: starting from subclassing wx.App.
And I stuck at setting logging facility.
To put it simply - here is the code. It works but LogWindow did not show up. What is the proper program workflow to configure logging system and how to use wx.LogWindow?
Also, I have found in docs about wx.AppTraits but there is not wx.App.CreateTraits function.

# coding: utf-8

import wx
import locale
import gui01 as gui

class Application(wx.App):
        wx.App.__init__(self, redirect = True)
        self.main_window = None

    def OnInit(self):
        target = wx.LogWindow(None, u"Журнал повідомлень додатка", show = True)
        target.PassMessages(False)
        wx.Log.SetActiveTarget(target)
        wx.LogError(u"Встановлено віконний журнал повідомлень.")
        self.main_window = gui.MainWindow()
        self.SetTopWindow(self.main_window)
        self.main_window.Show()
        wx.LogMessage(u"Створено головне вікно.")
        return True

    def InitLocale(self):
        lang, enc = locale.getdefaultlocale()
        self._initial_locale = wx.Locale(lang, lang[:2], lang)
        locale.setlocale(locale.LC_ALL, lang.replace("_", "-"))


if __name__ == "__main__":
    Application().MainLoop()

Windows 7
Python 3.8
wxPython 4.1.1

Hi,

In the OnInit() method you assign the wx.LogWindow to a local variable target which goes out of scope when the method returns.

Try assigning it to an instance attribute e.g. self.target instead.

Thanks, it really works. But after a little bit of research I even more confused. I even have have to use AI chat to find whether I am missing something trivial and to not look like complete idiot.
Here is whats the point.
wx.Log.SetActiveTarget has a word “set” which I assume means that function assign new ActiveTarget object to some internal variable. And in my logic it means that it increase reference counter of the new object, especially if it’s PyObject. Isn’t C binding not responsible for doing it?

For example:
this does not work

wx.Log.SetActiveTarget(wx.LogWindow(None, u"Loggin", show = True))

but this works

wx.Log.SetActiveTarget(wx.LogStderr())

And it’s turned out that wx.LogStderr() is already initialized and exist. That’s why it do no need any more references.

PS: and where is wx.App.CreateTraits?

Sorry, but I don’t think I can be of further help as I’ve never actually used the wx.Log facilities. Also, I don’t think I had ever heard of AppTraits until you mentioned it.

Where I used to work before I retired we had our own bespoke logging system for C-code, so we just interfaced to that from the python applications.

In my own applications I don’t need anything fancy, so I just use the python logging facility to write messages to each application’s log file. In some of the applications, the user can display the contents of the logfile in a simple dialog.

It seems you might want to create an instance of the wx.LogWindow outside of the call to SetActiveTarget and then pass it to the call, so that it doesn’t go out of scope.