Advise on using logging.getLogger needed.

Gelonida N gelonida at gmail.com
Mon Oct 3 18:51:48 EDT 2011


On 10/03/2011 12:12 AM, Steven W. Orr wrote:
> I hope I don't sound like I'm ranting :-(
> 
> I have created a module (called xlogging) which sets up logging the way I want
> it. I found out that if I set up my logger without a name, then it gets
> applied to every logger that is referenced by every module that ever gets
> imported.
> 
> The problem is that I must call xlogging.getLogger or logging.getLogger in all
> other modules using the name of the root logger, and I don't know what that
> name is.
> 
. . . .
> 
> My modules are used by multiple programs. Does this mean that I should simply
> use __name__ all the time? (That seems inelegant, no?)
> 
> If I'm making sense, is there a way to do this?
> 
> 

My impression is, that you try to use logging in a rather unconventional
way.

What you should normally do:

Only your main script should configure the logging.

If you want to do logging already during the import phase, then make
sure, that you configure logging importing any module, that wants to log
during the import phase.

All other modules  should only have following lines


import logging
log = logging.getLogger('name') # normally unique name for each module

if you have groups of modules, that belong together, then choose
hierarchical names
for example
'gui.mainwindow'
'gui.subwindow'

or similiar



So if xlogging.py is the module, that sets up logging the way you want,
then you should import it ONLY in your main script.
or perhaps in a 'if __name__ == '__main__' section for anything, that
you might want to run stand alone

you can configure obtain (and thus configure)
the root logger with
rootlogger =  logging.getLogger()

your module  can also configure other loggers and configure them.

gui_logger = logging.getlogger('gui')
configuring this logger will affect the configuration of all loggers
named 'gui' or 'gui.somethingelse'



You might want to look at the python doc to see how you could configure
logging via a config file
http://docs.python.org/release/2.6/library/logging.html#configuring-logging

or you can look at:
http://antonym.org/2005/03/a-real-python-logging-example.html






More information about the Python-list mailing list