How should logging be used in a Python package? -


i developing package can used without writing new code , modules can used develop new code (see documentation).

many of modules use logging in simple way:

import logging import sys logging.basicconfig(format='%(asctime)s %(levelname)s %(message)s',                     level=logging.debug,                     stream=sys.stdout) # code of module follows 

i have these lines in many modules. seems me should factor out, not sure best / recommended / pythonic way is.

the relevant parts in logging documentation seem configuring logging library , logging multiple modules.

i guess should move line logging.basicconfig executables (bin/hwrt) , remove other logging.basicconfig lines.

is there rule how packages should use logging (like pep8 coding style)?

if other developers use code might want disable / modify way package logging (so doesn't mixed logging calls). there way them so?

your library should not configure logging; that's application-wide task.

instead, set logger object based on __name__, use that, , that's it. document use logging, , developers using library can use standard logging api configure logging.

you add null handler root logger (the logger registered package name) prevent default configuration being used if application didn't set one:

# goes library somewhere logging.getlogger('name.of.library').addhandler(logging.nullhandler()) 

and developers using library can disable logging just library disabling log propagation:

logging.getlogger('name.of.library').propagate = false 

all documented in logging module; can consider style guide python logging. configuring logging library section linked to:

note: advised not add handlers other nullhandler library’s loggers. because configuration of handlers prerogative of application developer uses library. application developer knows target audience , handlers appropriate application: if add handlers ‘under hood’, might interfere ability carry out unit tests , deliver logs suit requirements.

logging.basicconfig() that; creates handler configuration.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -