Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python logging question

3 views
Skip to first unread message

koran...@gmail.com

unread,
Jan 2, 2009, 6:31:17 AM1/2/09
to
Hi,
I was reading through Python Logging tutorial, and I found one
scenario which I couldnt properly understand.
The tutorial (http://docs.python.org/library/logging.html)
mentions at first that -- "Multiple calls to getLogger() with the same
name will return a reference to the same logger object".

In an example for Python Logging Adapter, the tutorial then
mentions that -
"While it might be tempting to create Logger instances on a per-
connection basis, this is not a good idea because these instances are
not garbage collected".

I am confused reading both together. I will try to explain my
confusion with an example:

basicLogger = logging.getLogger("basic")

Class A():
def __init__(self):
self.logger = logging.getLogger("basic.class_a")

Now, I make say 10 instances of A and then delete one by one.

My understanding was that since the same name is used, a single
basic.class_a logger object is created inside the logging system, and
any calls to getLogger("basic.class_a") would return the same object
everytime.

So, my confusion is based on the second tutorial item I mentioned -
why is it not a good idea to create logger instances on a per-instance
basis? We are not creating new instances, right? And, if I create an
instance of A, it will be garbage collected later, right?

Could somebody help me out?

Vinay Sajip

unread,
Jan 2, 2009, 8:21:35 AM1/2/09
to
On Jan 2, 11:31 am, koranth...@gmail.com wrote:
> I am confused reading both together. I will try to explain my
> confusion with an example:
>
> basicLogger =logging.getLogger("basic")
>
> Class A():
> def __init__(self):
> self.logger =logging.getLogger("basic.class_a")

>
> Now, I make say 10 instances of A and then delete one by one.
>
> My understanding was that since the same name is used, a single
> basic.class_a logger object is created inside theloggingsystem, and

> any calls to getLogger("basic.class_a") would return the same object
> everytime.

That is correct. The logger instances stay around for the life of the
process, and are not garbage collected.

> So, my confusion is based on the second tutorial item I mentioned -
> why is it not a good idea to create logger instances on a per-instance
> basis? We are not creating new instances, right? And, if I create an
> instance of A, it will be garbage collected later, right?
>

It's not a problem to create loggers per *class*, as in your example.
It can be a bad idea to create different logger per class *instances*.
The second example in the docs talks about creating loggers on a per-
connection basis in a networked app. This is not per connection class,
mind you, but per connection instance. You would typically have only a
few dozen classes, but you might have hundreds of thousands of
connection instances created in a long-lived server app. If you
created a unique logger for each connection, for example based on the
time the connection was instantiated - e.g. with name "connection.
20090102123456543", this would create hundreds of thousands of unique
logger instances and have a potentially adverse impact on process
memory. That's when you use LoggerAdapters.

I hope that's clearer.

Regards,

Vinay Sajip

koran...@gmail.com

unread,
Jan 2, 2009, 8:38:41 AM1/2/09
to

Thank you very much Vinay.
I was confused by the way it was mentioned in the tutorial.
Again, Thank you
Regards
Koranthala

0 new messages