Using logging in multiple modules
A simple way of using one instance of logging library in multiple modules for me was following solution:
base_logger.py
import logging
logger = logging
logger.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
Other files
from base_logger import logger
if __name__ == '__main__':
logger.info("This is an info message")
Best practice is, in each module, to have a logger defined like this:
import logging
logger = logging.getLogger(__name__)
near the top of the module, and then in other code in the module do e.g.
logger.debug('My message with %s', 'variable data')
If you need to subdivide logging activity inside a module, use e.g.
loggerA = logging.getLogger(__name__ + '.A')
loggerB = logging.getLogger(__name__ + '.B')
and log to loggerA
and loggerB
as appropriate.
In your main program or programs, do e.g.:
def main():
"your program code"
if __name__ == '__main__':
import logging.config
logging.config.fileConfig('/path/to/logging.conf')
main()
or
def main():
import logging.config
logging.config.fileConfig('/path/to/logging.conf')
# your program code
if __name__ == '__main__':
main()
See here for logging from multiple modules, and here for logging configuration for code which will be used as a library module by other code.
Update: When calling fileConfig()
, you may want to specify disable_existing_loggers=False
if you're using Python 2.6 or later (see the docs for more information). The default value is True
for backward compatibility, which causes all existing loggers to be disabled by fileConfig()
unless they or their ancestor are explicitly named in the configuration. With the value set to False
, existing loggers are left alone. If using Python 2.7/Python 3.2 or later, you may wish to consider the dictConfig()
API which is better than fileConfig()
as it gives more control over the configuration.
Actually every logger is a child of the parent's package logger (i.e. package.subpackage.module
inherits configuration from package.subpackage)
, so all you need to do is just to configure the root logger. This can be achieved by logging.config.fileConfig
(your own config for loggers) or logging.basicConfig
(sets the root logger). Setup logging in your entry module (__main__.py
or whatever you want to run, for example main_script.py
. __init__.py
works as well)
using basicConfig:
# package/__main__.py
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
using fileConfig:
# package/__main__.py
import logging
import logging.config
logging.config.fileConfig('logging.conf')
and then create every logger using:
# package/submodule.py
# or
# package/subpackage/submodule.py
import logging
log = logging.getLogger(__name__)
log.info("Hello logging!")
For more information see Advanced Logging Tutorial.