Logging in Python

Discussion in 'Python' started by aha, Feb 10, 2009.

  1. aha

    aha Guest

    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?

    Aquil
     
    aha, Feb 10, 2009
    #1
    1. Advertising

  2. aha

    Robert Kern Guest

    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
     
    Robert Kern, Feb 10, 2009
    #2
    1. Advertising

  3. aha

    Vinay Sajip Guest

    On Feb 10, 5:50 pm, aha <> wrote:
    > Hello All,
    >
    > I have an application whereloggingmay need to be configured in
    > multiple places. I've used the PythonLoggingFramework for sometime,
    > but I'm still not sure how to test iflogginghas configured. For
    > example, I have modules A, B, and C.
    >
    > Below is some pseudo code...
    > moduleA
    >
    > class A(object):
    > def __init__(self):
    > ...
    >
    > startLogging(config):
    > # Configurelogging
    > # global logger
    > ...
    >
    > moduleB
    > import moduleA
    > from myconfig import MyConfig
    > class B(object):
    > def __init__(self):
    > # self.config = MyConfig()
    > # iflogginghas 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?
    >
    > Aquil


    It depends upon how complicated your logging requirements are. For
    example, each module can have the following code in it:

    import logging

    logging.basicConfig(level=logging.DEBUG, filename="/tmp/myapp.log",
    filemode="w") # An example

    logger = logging.getLogger(__name__)

    .... your code, involving logger.debug(...) statements


    basicConfig() attaches a FileLogger to the root logger, so all logging
    output would be routed to the file "/tmp/myapp.log" in the example.
    However, basicConfig() does nothing if the root logger already has
    handlers, so calling it in each module shouldn't cause problems. It's
    also nice to use the module name (__name__) as the logger name.

    Another pattern is to configure logging in your main module, if there
    is one, and then the other modules just assume logging is configured
    and log away. If there isn't a main module, have all the modules
    import a common module which, when imported, configures logging how
    you want it. Under normal circumstances, the import code will only run
    once, so your logging only gets configured the first time the module
    gets imported by any of the others.

    Regards,

    Vinay Sajip
     
    Vinay Sajip, Feb 10, 2009
    #3
  4. aha

    aha Guest

    Thanks for your suggestions. I've also figured that I can test if
    logging.RootLogger.manager.loggerDict has any items in it. Or if it
    has a logger for the module that I wish to start. I like basicLogger
    idea though as it seems like the cleanest implementation.

    On Feb 10, 3:21 pm, Vinay Sajip <> wrote:
    > On Feb 10, 5:50 pm, aha <> wrote:
    >
    >
    >
    > > Hello All,

    >
    > > I have an application whereloggingmay need to be configured in
    > > multiple places.  I've used the PythonLoggingFramework for sometime,
    > > but I'm still not sure how to test iflogginghas configured.  For
    > > example, I have modules A, B, and C.

    >
    > > Below is some pseudo code...
    > > moduleA

    >
    > > class A(object):
    > >   def __init__(self):
    > >     ...

    >
    > > startLogging(config):
    > >   # Configurelogging
    > >   # global logger
    > >   ...

    >
    > > moduleB
    > > import moduleA
    > > from myconfig import MyConfig
    > > class B(object):
    > >   def __init__(self):
    > >     # self.config = MyConfig()
    > >     # iflogginghas 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?

    >
    > > Aquil

    >
    > It depends upon how complicated your logging requirements are. For
    > example, each module can have the following code in it:
    >
    > import logging
    >
    > logging.basicConfig(level=logging.DEBUG, filename="/tmp/myapp.log",
    > filemode="w") # An example
    >
    > logger = logging.getLogger(__name__)
    >
    > ... your code, involving logger.debug(...) statements
    >
    > basicConfig() attaches a FileLogger to the root logger, so all logging
    > output would be routed to the file "/tmp/myapp.log" in the example.
    > However, basicConfig() does nothing if the root logger already has
    > handlers, so calling it in each module shouldn't cause problems. It's
    > also nice to use the module name (__name__) as the logger name.
    >
    > Another pattern is to configure logging in your main module, if there
    > is one, and then the other modules just assume logging is configured
    > and log away. If there isn't a main module, have all the modules
    > import a common module which, when imported, configures logging how
    > you want it. Under normal circumstances, the import code will only run
    > once, so your logging only gets configured the first time the module
    > gets imported by any of the others.
    >
    > Regards,
    >
    > Vinay Sajip
     
    aha, Feb 10, 2009
    #4
  5. aha

    Vinay Sajip Guest

    On Feb 10, 9:38 pm, aha <> wrote:
    > Thanks for your suggestions. I've also figured that I can test iflogging.RootLogger.manager.loggerDict has any items in it. Or if it
    > has a logger for the module that I wish to start. I like basicLogger
    > idea though as it seems like the cleanest implementation.
    >


    It's best not to dig into the implementation details, as these might
    change across versions of Python. Any of the suggestions that Robert
    and I made should be able to solve your problem in the right way.

    Best regards,

    Vinay Sajip
     
    Vinay Sajip, Feb 10, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Siegl
    Replies:
    0
    Views:
    963
    Stefan Siegl
    Aug 27, 2003
  2. janne
    Replies:
    0
    Views:
    9,509
    janne
    Sep 10, 2004
  3. Christoph Haas
    Replies:
    0
    Views:
    457
    Christoph Haas
    Jun 12, 2006
  4. Christoph Haas
    Replies:
    1
    Views:
    462
    Vinay Sajip
    Jun 14, 2006
  5. johnny
    Replies:
    1
    Views:
    651
    Dennis Lee Bieber
    Dec 12, 2006
Loading...

Share This Page