I have a package that has several components in it that would benefit greatly from using logging and outputting useful information.

What I do not want to do is to 'setup' proper logging for every single file with somewhere along these lines:

import logging
logging.basicConfig(level=DEBUG)
my_function = logging.getLogger("my_function")
my_class = logging.getLogger("my_class")

I have tried a couple of approaches, one of them being adding the boilerplate code into a class within a utility module and try and do something like this:

from util import setlogging
set_logging()

But even the above solution doesn't look clean to me and would cause issues because setLogger doesn't have a __call__ method. What I did liked was that my "set_logging" class would read form a config file and have some default values so it wouldn't matter what level or what type of logging format I wanted it would set it up correctly.

Is there a way to initialize proper logging across the board in my package? Maybe in the __init__.py file?

And just to be as verbose as possible, this is what setlogging (now a function, not a class) looks like:

def setlogging(config=None):
    if config == None:
        config = config_options() # sets default values
    levels = {
        'debug': DEBUG,
       'info': INFO
        }

    level = levels.get(config['log_level'])
    log_format = config['log_format']
    datefmt = config['log_datefmt']

    basicConfig(
        level   = level,
        format  = log_format,
        datefmt = datefmt)
Edit
Report