From: genkuro on
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
From: Mark Lawrence on
On 15/06/2010 16:35, 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

Change it to something like logger = logging.getLogger(__name__), then
logger.addHandler(x). If you don't do this, your logging shadows the
logging module so you won't get very far.

HTH.

Mark Lawrence

From: Peter Otten on
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__)

Dubious indeed. As a workaround you can import the module again, preferably
under another name:

import logging as real_logging_module

You can think of

import logging

as a shortcut for

logging = __import__("logging")

and of

import logging as x

as a shortcut for

x = __import__("logging")

> 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.

Hmm, this shouldn't fail, as there is a Logger.addHandler() method whereas
the logging module doesn't contain an addHandler() function:

>>> import logging
>>> logging.addHandler
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'addHandler'
>>> logging.getLogger().addHandler
<bound method RootLogger.addHandler of <logging.RootLogger instance at
0x7f06bdc71a28>>

Peter
From: Paul Rudin on
Peter Otten <__peter__(a)web.de> writes:

> 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__)
>
> Dubious indeed. As a workaround you can import the module again, preferably
> under another name:

It's better just to use another variable name - e.g. "logger", Shirley?
From: genkuro on
On Jun 15, 8:49 am, Mark Lawrence <breamore...(a)yahoo.co.uk> wrote:
> On 15/06/2010 16:35, 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
>
> Change it to something like logger = logging.getLogger(__name__), then
> logger.addHandler(x).  If you don't do this, your logging shadows the
> logging module so you won't get very far.
>
> HTH.
>
> Mark Lawrence

Hi Mark -

I thought that would be the answer.

I asked because I'm working with a framework where logging is
similarly renamed in almost every file. The framework is under
development so refactoring is an option.

I'm coming to Python from Java. I'm still getting a feel for scoping
limits. For the sake of curiosity, is there another way to refer to a
package besides name?

Thanks,
Brian