Package: logconfig
Simple helper moudle for configuring Python logging.RequirementsCompatibilityPython 2.6Python 2.7Python 3.2Python 3.3Python 3.4DependenciesPyYAMLlogutils (if using Python 2)Installationpip install logconfigOverviewThis simple library exposes several helper methods for configuring the standard library’sloggingmodule. There’s nothing fancy about it. Under the hoodlogconfiguseslogging.configto load various configuartion formats.In addition to configuration loading,logconfigprovides helpers for easily converting a configured logger’s handlers utilize a queue.Supported Configuration FormatsJSONYAMLConfigParserPython DictQuickstartConfiguration Loadingimportlogconfigimportlogging# Load config from JSON filelogconfig.from_json('path/to/file.json')# Load config from YAML filelogconfig.from_yaml('path/to/file.yml')# Load config from ConfigParser filelogconfig.from_yaml('path/to/file.cfg')# Load config from dictlogconfig.from_dict(config_dict)log=logging.getLogger()log.debug('Configuration loaded using logconfig')Queue Utilizationimportlogconfigimportlogginglogconfig.from_dict({'version':1,'disable_existing_loggers':False,'handlers':{'console':{'class':'logging.StreamHandler','level':'DEBUG'}},'loggers':{'mylogger':{'handlers':['console']}}})# Convert logger's handlers to utilize a queuequeue=logconfig.Queue(-1)listener=logconfig.QueueListener(queue)handler=logconfig.QueueHandler(queue)mylogger=logging.getLogger('mylogger')# You can also pass in the logger name instead of the actual logger.# logconfig.queuify_logger('mylogger', handler, listener)logconfig.queuify_logger(mylogger,handler,listener)assertisinstance(mylogger.handlers[0],logconfig.QueueHandler)# Start the listener.listener.start()# When finished, stop the listener.# This is optional, but not doing so may prevent some logs from being processed.listener.stop()UsageUselogconfigto easily loadloggingconfigurations. For more details on configuringlogging, visithttps://docs.python.org/library/logging.config.html.importlogconfigConfiguration from JSONConfigure logging using JSON file.logconfig.from_json(filename)Example JSON file:{"version":1,"disable_existing_loggers":false,"formatters":{"simple":{"format":"%(asctime)s. - %(name)s - %(levelname)s - %(message)s"}},"handlers":{"console":{"class":"logging.StreamHandler","level":"DEBUG","formatter":"simple","stream":"ext://sys.stdout"}},"root":{"level":"DEBUG","handlers":["console"]}}Configuration from YAMLConfigure logging using YAML file.logconfig.from_yaml(filename)Example YAML file:version:1disable_existing_loggers:Falseformatters:simple:format:"%(asctime)s.-%(name)s-%(levelname)s-%(message)s"handlers:console:class:logging.StreamHandlerlevel:DEBUGformatter:simplestream:ext://sys.stdoutroot:level:DEBUGhandlers:[console]Configuration from ConfigParser FileConfigure logging using ConfigParser compatible file.logconfig.from_file(filename)Example CFG file:[loggers]keys=root[handlers]keys=console[formatters]keys=simple[logger_root]level=DEBUGhandlers=console[handler_console]class=StreamHandlerlevel=DEBUGformatter=simpleargs=(sys.stdout,)[formatter_simple]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sConfiguration from DictConfigure logging using Python dictionary.logconfig.from_dict(dct)Example dict:{'version':1,'disable_existing_loggers':False,'formatters':{'simple':{'format':'%(asctime)s. -%(name)s-%(levelname)s-%(message)s'}},'handlers':{'console':{'formatter':'simple','class':'logging.StreamHandler','level':'DEBUG','stream':'ext://sys.stdout'}},'root':{'handlers':['console'],'level':'DEBUG'}}Configuration from AutodetectionIf, for whatever reason, you do not know what the source of the configuration will be (or if you’re just feeling lucky), then you can try to coerce logging configuration using one of the autodetection methods:logconfig.from_filename(filename)logconfig.from_autodetect(filename_or_dict)try:logconfig.from_filename(filename)logconfig.from_autodetect(filename_or_dict)exceptlogconfig.LogConfigExceptionasex:# Unrecognized configuration argument.passThese methods will try to dispatch the function argument to the proper configuration loader or fail trying.Configuration from Environment VariableConfigure logging using filename provided via environment variable.logconfig.from_env(variable_name)NOTE:Environment variable value will be passed tofrom_filename().