Logging in Python

A

aha

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
 
R

Robert Kern

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
 
V

Vinay Sajip

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
 
A

aha

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.

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?

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
 
V

Vinay Sajip

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top