FAQ
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?

Search Discussions

  • Vinay Sajip at Jan 2, 2009 at 1:21 pm

    On Jan 2, 11:31 am, koranth... at 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
  • Koranthala at Jan 2, 2009 at 1:38 pm

    On Jan 2, 6:21?pm, Vinay Sajip wrote:
    On Jan 2, 11:31 am, koranth... at 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
    Thank you very much Vinay.
    I was confused by the way it was mentioned in the tutorial.
    Again, Thank you
    Regards
    Koranthala

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 2, '09 at 11:31a
activeJan 2, '09 at 1:38p
posts3
users2
websitepython.org

2 users in discussion

Koranthala: 2 posts Vinay Sajip: 1 post

People

Translate

site design / logo © 2022 Grokbase