Logging in Python

Robert Kern robert.kern at gmail.com
Tue Feb 10 15:18:55 EST 2009


On 2009-02-10 11:50, aha wrote:
> Hello All,
>
> I have an application where logging may need to be configured in
> multiple places.  I've used the Python Logging Framework for sometime,
> but I'm still not sure how to test if logging has configured.  For
> example, I have modules A, B, and C.
>
> Below is some pseudo code...
> moduleA
>
> class A(object):
>    def __init__(self):
>      ...
>
> startLogging(config):
>    # Configure logging
>    # global logger
>    ...
>
> moduleB
> import moduleA
> from myconfig import MyConfig
> class B(object):
>    def __init__(self):
>      # self.config = MyConfig()
>      # if logging has started [HOW DO YOU DO THIS?]
>      #   self.logger = logging.getLogger("moduleB")
>      # else
>      #   self.logger = moduleA.startLogging(self.config)
>      # moduleA.startLogging
>      ...
>
> Where I need help is determining if a logger has already been
> configured.  Any advice?

I just do this in every module in which I'm doing logging:

   import logging
   logger = logging.getLogger(__name__)

I configure my logging only in my main() function(s).

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list