Package: simple-logging-setup
simple-logging-setupsimple-logging-setup is a simple yet highly configurable library that provides
sane configuration defaults for the logging system in the Python
standard library.Features:No external dependenciesSimple coloring and detection if the terminal supports colored outputFormatting for exceptionsSupport forsyslog severity levelsLog filteringEasy to integrate into command line optionsInstallationsimple-logging-setup can be installed using pippip install simple-logging-setupUsageSimple Setupimportloggingfromsimple_logging_setupimportsetuplogger=logging.getLogger('my-logger')setup(level='info',# put your configuration here)logger.info('Hi mom!')Setup using command line optionsimportargparsefromsimple_logging_setupimportsetup# parse command lineparser=argparse.ArgumentParser()parser.add_argument('-l','--log-level',choices=['debug','info','warn','error','critical'],default='info',)parser.add_argument('--loggers',type=str,nargs='+',)parser.add_argument('--show-timestamps',action='store_true',)args=parser.parse_args()# setup loggingsetup(level=args.log_level,loggers=args.loggers,show_timestamp=args.show_timestamps,)$ my-tool -l debug --loggers my-logger -disabled-logger
$ my-tool --show-timestampsConfigurationAll configuration is done by adding keyword arguments tosimple_logging_setup.setup.NameTypeDefaultDescriptionlevelstrinfoLog level. Choices (case insensitive):['debug', 'info', 'warn', 'warning', 'error', 'critical']colorsswitchTrueEnables or disables colors. Gets disabled by default if the terminal does not support colored outputsyslog_prioritiesswitchFalseEnables or disablessyslog severity levels. Gets enabled by default if running in journaldshow_thread_nameswitchTrueShow thread name in log outputshow_level_nameswitchTrueShow log level name in log output. Gets enabled by default if colors are disabled or not availableshow_timestampswitchTrueShow timestamp in log outputshow_logger_nameswitchTrueShow logger name in log outputswitchis a special type that does best-effort parsing of incoming values.Examples:True,1,'True','TRUE','yes','on', [...]FilteringEspecially when the log level is set todebug, logging output can become hard
to read. simple-logging-setup allows you to filter loggers by including or
excluding specific logger names.fromsimple_logging_setupimportsetupsetup(loggers=[# include loggers to the output (the `+` is optional)'my-project.logger-1','+my-project.logger-1',# exclude loggers from the output (both `-` and `_` can be used)'-my-project.logger-1','_my-project.logger-1',],)Why two prefixes to choose for excludes? The-notation makes more sense in
code but may not be suitable for command line options. This optional extra
notation allows for command line options like--loggers +included-logger _excluded-logger.Name FilteringIn some cases, it is not useful to always print all logger names. For example,
when creating a command line tool, a logger name likemy-project.my-sub-systemis worth printing, butrootis not. simple-logging-setup allows you to filter
out logger names, without excluding the logger entirely.fromsimple_logging_setupimportsetupsetup(filter_logger_names=['root'],)Presetssimple-logging-setup comes with a list of useful presets which can be enabled
via thepresetkeyword argument.fromsimple_logging_setupimportsetupsetup(preset='service')# 'service' is the defaultNameDescriptionserviceEnables all switches and features availablecliDisables all switches butshow_logger_nameand filters the logger nameroot