logging = logging.getLogger(__name__)

G

genkuro

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
 
M

Mark Lawrence

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
 
P

Peter Otten

genkuro said:
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:
Traceback (most recent call last):
<bound method RootLogger.addHandler of <logging.RootLogger instance at
0x7f06bdc71a28>>

Peter
 
P

Paul Rudin

Peter Otten said:
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?
 
G

genkuro

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
 
D

Dave Angel

genkuro said:
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
 
J

Jean-Michel Pichavant

genkuro said:
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
 
M

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

Stephen Hansen

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/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMF6mdAAoJEKcbwptVWx/lR1AH/Rd8jYEadfTxFWgyGwyEih8C
uMiNEbNMig40+SquZTBAaCOLJpBQ4R1Nygf717r6vrJeMqK/e1ATruHl6eEAMdnI
kfPtdEE1IQCxrblrVkYssoOrjYcJnXNq0FKUamSU89Kbvaj5iMUxVzSAEyeD3nbC
KFmAffjWu1Mw63J+iPV/wKVXK9N4PPfdoTA/SRH6lj11salFgrQDfNR09REVa7XG
2qIUAC5QvYr45Ob5+tVvNTn+VLp8nkfJvv8ecK0k2ynRO1HnudcFjwOFHzbqKPL0
9OmOhtsugJoPa9cmWAacxOsBdzgcINyRWSnpvx4VJ7z5LlcCiwXxmGfFvj3rmVA=
=k85Z
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top