Python logging question

Vinay Sajip vinay_sajip at
Fri Jan 2 14:21:35 CET 2009

On Jan 2, 11:31 am, koranth... at 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.


Vinay Sajip

More information about the Python-list mailing list