Advise on using logging.getLogger needed.

Steven W. Orr steveo at syslang.net
Sun Oct 2 18:12:03 EDT 2011


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.

I want the name of the root logger to be the name of the running program, so
in my setup, I say

            pname = sys.argv[0]
            try:
                rslash = pname.rindex('/')
            except ValueError:
                pass
            else:
                pname = pname[rslash + 1:]

            try:
                dot_py = pname.rindex('.py')
            except ValueError:
                pass
            else:
                pname = pname[0:dot_py]

and then I say

            logger = logging.getLogger(pname)

My problem is that I just want the modules that I write, to be able to get the
 named root logger and still be able to refer to possible sub-loggers.

So, let's say I run program foo. At some point in my mail setup, I set the
configuration for the foo logger. I'd like my xlogging module to supply a
getLogger function such that if I don't supply any arguments, it will return
the logger that would normally be returned by logging.getLogger(pname).

Also, part of the reason I'm confused is because foo calls m1 and m2 and they
want to say

logger = xlogging.getLogger(MaybeWithAnArg?)

at the top of their code. This means that the import statement for m1 and m2
are getting the calls to getLogger executed before I get the chance to set
things up.

I am running 2.6, so I don't have access to logging.getChild, but I'm not
clear that I even want that.

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?


-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



More information about the Python-list mailing list