logging = logging.getLogger(__name__)

Dave Angel davea at ieee.org
Tue Jun 15 12:13:46 EDT 2010


genkuro wrote:
> Newbie here.  I may be missing something obvious, in which case,
> please feel free to berate and laugh at me.
>
> Here's a dubious line of code:
> logging = logging.getLogger(__name__)
>
> How can I refer to the original logging package "logging" after this
> statement is run?  Specifically, I'm trying to add a log handler with
> logging.addHandler(x) and it is of course failing.
>
> Thanks,
> Brian
>
>   
Welcome to the forum, and to Python.
Excellent question.  Simple answer is to change the left side to 
something like  "logger="   And use that as your logger.

However, if you already have lots of code (written by someone else, 
presumably), and don't want to change all the other references to that 
name, you could do something like (ugly):

import logging as loggingmodule
logging = loggingmodule.getLogger(...

Then you can continue to use loggingmodule to refer to the module.

The reason I don't encourage this is that whenever you look in the docs, 
you'll see a reference to something like logging.addHandler, and you'll 
have to remember to change it to
    loggingmodule.addHandler

HTH,
DaveA




More information about the Python-list mailing list