[Python-ideas] logging.config.defaultConfig()

Thomas Güttler guettli at thomas-guettler.de
Fri May 16 09:05:11 CEST 2014


Using logging (as library) is easy in Python.

But setting up logging is done different in nearly every environment. I would like to have
a common way to **load** the configuration. The configuration itself should be done
in the environment the scripts runs in.

If you write a console script, and you want it to be reusable in different environments, there
is no default way at the moment (or at least I don't see any) to let the environment
set up the logging.

I want a standard hook: The console script should be able to call into the surrounding environment.
This improves reusablity.

I know how to use dictConfig() or fileConfig(), but these methods need parameters. And what
the parameters look like should not be defined in the reusable console script.

I think the following solution is very flexible and solves most needs to set up logging,
since I can implement your needs in for example your_environment_module.set_up()


{{{
def defaultConfig():
    '''
    Load module to set_up() the logging configuration of your environment.

    Reads the module name from: os.environ.get('LOGGINGCONFIG', 'loggingconfig')

    Calls set_up() on the imported module.

    Would be nice to have this as logging.config.defaultConfig

    Related: https://docs.python.org/2/library/logging.config.html

    '''
    module_name = os.environ.get('LOGGINGCONFIG', 'loggingconfig')
    module = importlib.import_module(module_name)
    module.set_up()
}}}

Do you understand what I propose?

What do you think?

  Thomas Güttler

-- 
Thomas Güttler
http://thomas-guettler.de/


More information about the Python-ideas mailing list