Logging in Python

Vinay Sajip vinay_sajip at yahoo.co.uk
Tue Feb 10 15:21:42 EST 2009


On Feb 10, 5:50 pm, aha <aquil.abdul... at gmail.com> 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



More information about the Python-list mailing list