From: Dave Angel 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__)
>
> 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

From: Jean-Michel Pichavant on
genkuro wrote:
> 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
>
>
Yes, there is another way but you don't want to do that. As mentioned
before, use a proper name (logger is a good candidate).

import sys
print sys.modules['logging']
<module 'logging' from '/usr/lib/python2.5/logging/__init__.pyc'>

pylint usually tells you when you're shadowing some standard package, I
would advise to use this tool (requires some tuning though).

JM
From: Mark Lawrence on
On 15/06/2010 17:03, genkuro wrote:
> 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
>

Peter Otten has already pointed out that you can use import logging as
x, but I don't see the need for it in this instance. Paul Rudin has
agreed with me, i.e. just use logger, so why not refactor your code now,
I think that you'll appeciate it in the long term.

Cheers.

Mark Lawrence.

From: Stephen Hansen on
On 6/15/10 9:03 AM, genkuro wrote:
> 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?

The only way to refer to anything is by its name -- or, from a name and
through subscript/dot notation if you've stored something in a container.

But a package (and anything else) can have many names, and it really has
no idea what they are.

You can do "import logging as logging_package" which is just a shortcut for:

import logging
logging_package = logging
del logging

And such.

That said: The frameworks I've seen that shadow the global 'logging'
package with a specific logger do so somewhat on purpose (though I find
the practice slightly dubious), so that naive code which previously just
used the general root logger would work with the more specific one
seamlessly.

But such frameworks usually also have a setup/environment sort of file
where this is not done, and that's where things like adding handlers
belongs.

Just as an aside.

Renaming "logging = ..." to "logger = ..." is probably a better solution
anyways :)

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/