wxPython logging vs python logging

877 views
Skip to first unread message

Igor Korot

unread,
Aug 7, 2013, 8:46:01 PM8/7/13
to wxpytho...@googlegroups.com
Hi, ALL,
This is my first post here as I just started with wxPython.
 
Here is what I am looking for:
 
I designed a script using wxPython GUI. In this script I collect some data and when the user clicks the button I want to execute another script
that I imported. That second script has following lines in the beginning of the main function:
 
format_str = '[%(levelname)s] %(message)s'
logging.basicConfig(level=logging.INFO, format=format_str)
 
Now what I am trying to do is start a dialog with text control, execute the external script and the logging that this secondary script will produce
should go to my text control inside dialog.
 
It would be nice if the dialog will be modal, but AFAICT it simply not possible to get inside modal dialog loop without some serious tweaks. So for now
the modeless dialog will suffice. The problem comes from the fact that I don't have too much experience neither with logging inside wxWidgets and even
less with python.
 
So any guidance in this matter will be really helpful.
 
Thank you.

 

werner

unread,
Aug 8, 2013, 3:14:26 AM8/8/13
to wxpytho...@googlegroups.com
Hi Igor,

On 08/08/2013 02:46, Igor Korot wrote:
> Hi, ALL,
> This is my first post here as I just started with wxPython.
Welcome!
> Here is what I am looking for:
> I designed a script using wxPython GUI. In this script I collect some
> data and when the user clicks the button I want to execute another script
> that I imported. That second script has following lines in the
> beginning of the main function:
> format_str = '[%(levelname)s] %(message)s'
> logging.basicConfig(level=logging.INFO, format=format_str)
> Now what I am trying to do is start a dialog with text control,
> execute the external script and the logging that this secondary script
> will produce
> should go to my text control inside dialog.
You need to define a handler for your logging to write the log to a file
and then read it in from that file and write it to your text control.

You also might want to consider using a logging configuration file
instead of using basicConfig.

The Python 2.7 doc on logging is quit handy and I just came accros this
other tutorial.
http://docs.python.org/2/howto/logging.html
http://victorlin.me/2012/08/good-logging-practice-in-python/

Werner

Igor Korot

unread,
Aug 8, 2013, 3:38:22 AM8/8/13
to wxpytho...@googlegroups.com
Werner,

On Thu, Aug 8, 2013 at 12:14 AM, werner <wbr...@free.fr> wrote:
> Hi Igor,
>
>
> On 08/08/2013 02:46, Igor Korot wrote:
>>
>> Hi, ALL,
>> This is my first post here as I just started with wxPython.
>
> Welcome!

Thank you.
I'm just more used to C++ version of wx then wxPython. ;-)

>
>> Here is what I am looking for:
>> I designed a script using wxPython GUI. In this script I collect some data
>> and when the user clicks the button I want to execute another script
>> that I imported. That second script has following lines in the beginning
>> of the main function:
>> format_str = '[%(levelname)s] %(message)s'
>> logging.basicConfig(level=logging.INFO, format=format_str)
>> Now what I am trying to do is start a dialog with text control, execute
>> the external script and the logging that this secondary script will produce
>> should go to my text control inside dialog.
>
> You need to define a handler for your logging to write the log to a file and
> then read it in from that file and write it to your text control.

I wish there was an example of how to use python and wxPython logging
along with all
other wxPython demos/samples.

Now, is there a way to do a redirect for the python logger to become
my text control?
When I run the script from shell, I see a lot of messages that I'd
like to see spit in there.

>
> You also might want to consider using a logging configuration file instead
> of using basicConfig.

Thing is this external script is not made by me and it's external to
the tool I'm working with.
So, it would be great if it will not be changed at all to ease the
pain of maintaining our own version.

I will look it up though.

Thank you.

>
> The Python 2.7 doc on logging is quit handy and I just came accros this
> other tutorial.
> http://docs.python.org/2/howto/logging.html
> http://victorlin.me/2012/08/good-logging-practice-in-python/
>
> Werner
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "wxPython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to wxpython-user...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Igor Korot

unread,
Aug 8, 2013, 3:45:48 AM8/8/13
to wxpytho...@googlegroups.com
Or maybe there is a logging class I can use to redirect the logging?
In the wxPython documentation I saw a wx.LogTextCtrl class, but I
didn't look it up too close.

Maybe using this class will suffice?

Thank you.

werner

unread,
Aug 8, 2013, 3:57:53 AM8/8/13
to wxpytho...@googlegroups.com
Hi Igor,

On 08/08/2013 09:38, Igor Korot wrote:

>> You need to define a handler for your logging to write the log to a file and
>> then read it in from that file and write it to your text control.
> I wish there was an example of how to use python and wxPython logging
> along with all
> other wxPython demos/samples.
Yeap, that would be nice;-)
>
> Now, is there a way to do a redirect for the python logger to become
> my text control?
Never done it, but I guess one could create a custom handler which
instead of writing to a file writes to a textctrl.

This might get you started:
http://stackoverflow.com/questions/2689441/python-combine-logging-and-wx-so-that-logging-stream-is-redirectet-to-stdout-st
> When I run the script from shell, I see a lot of messages that I'd
> like to see spit in there.
>
>> You also might want to consider using a logging configuration file instead
>> of using basicConfig.
> Thing is this external script is not made by me and it's external to
> the tool I'm working with.
If this script is part of a "library" used by other tools then they
should not use basicConfig they should just do something like:

log = logging.getLogger('somename')
log.info(.....) etc.

and probably have an option to turn on logging which then could use the
basicConfig stuff if they don't want to use a config file.

But you should also be able to attach your handler to their logging
(even if they use basicConfig) by doing something like:

theirmodule.main.logging.addHandler........

Werner

Mike Driscoll

unread,
Aug 8, 2013, 4:18:22 PM8/8/13
to wxpytho...@googlegroups.com
Hi,

If the dialog is modal, it may block the other script unless the second script is running in a thread.

 
 
So any guidance in this matter will be really helpful.
 
Thank you.



I have a couple of articles that might help. Here's one on redirecting stdout:

http://www.blog.pythonlibrary.org/2009/01/01/wxpython-redirecting-stdout-stderr/

And here's one on logging:

http://www.blog.pythonlibrary.org/2012/08/02/python-101-an-intro-to-logging/

I also played around a bit to try to get the logging module's StreamHandler to redirect to my text control, but it had no interest in doing so. Thus I wrote a custom logging handler. I have attached my code. Hopefully it will help you get going.

- Mike
wxlogg.py

Mike Driscoll

unread,
Aug 8, 2013, 4:24:00 PM8/8/13
to wxpytho...@googlegroups.com
I should also note that I left my sys.stdout code in there just to show the OP how to redirect stdout. You actually can remove that code as it doesn't really do anything unless you have print statements in your code.

- Mike

Igor Korot

unread,
Aug 8, 2013, 9:35:25 PM8/8/13
to wxpytho...@googlegroups.com
Mike,
You code worked perfectly.
For now I can probably live with custom window with logging.

Thank you.

>
> - Mike
Reply all
Reply to author
Forward
0 new messages