Merge "Allow custom log levels for other loggers"
This commit is contained in:
commit
088cb82631
@ -18,6 +18,13 @@ import sys
|
|||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
|
def get_loggers():
|
||||||
|
loggers = {}
|
||||||
|
for logkey in logging.Logger.manager.loggerDict.keys():
|
||||||
|
loggers[logkey] = logging.getLevelName(logging.getLogger(logkey).level)
|
||||||
|
return loggers
|
||||||
|
|
||||||
|
|
||||||
def log_level_from_options(options):
|
def log_level_from_options(options):
|
||||||
# if --debug, --quiet or --verbose is not specified,
|
# if --debug, --quiet or --verbose is not specified,
|
||||||
# the default logging level is warning
|
# the default logging level is warning
|
||||||
@ -34,6 +41,17 @@ def log_level_from_options(options):
|
|||||||
return log_level
|
return log_level
|
||||||
|
|
||||||
|
|
||||||
|
def log_level_from_string(level_string):
|
||||||
|
log_level = {
|
||||||
|
'critical': logging.CRITICAL,
|
||||||
|
'error': logging.ERROR,
|
||||||
|
'warning': logging.WARNING,
|
||||||
|
'info': logging.INFO,
|
||||||
|
'debug': logging.DEBUG,
|
||||||
|
}.get(level_string, logging.WARNING)
|
||||||
|
return log_level
|
||||||
|
|
||||||
|
|
||||||
def log_level_from_config(config):
|
def log_level_from_config(config):
|
||||||
# Check the command line option
|
# Check the command line option
|
||||||
verbose_level = config.get('verbose_level')
|
verbose_level = config.get('verbose_level')
|
||||||
@ -49,15 +67,7 @@ def log_level_from_config(config):
|
|||||||
verbose_level = 'info'
|
verbose_level = 'info'
|
||||||
else:
|
else:
|
||||||
verbose_level = 'debug'
|
verbose_level = 'debug'
|
||||||
|
return log_level_from_string(verbose_level)
|
||||||
log_level = {
|
|
||||||
'critical': logging.CRITICAL,
|
|
||||||
'error': logging.ERROR,
|
|
||||||
'warning': logging.WARNING,
|
|
||||||
'info': logging.INFO,
|
|
||||||
'debug': logging.DEBUG,
|
|
||||||
}.get(verbose_level, logging.WARNING)
|
|
||||||
return log_level
|
|
||||||
|
|
||||||
|
|
||||||
def set_warning_filter(log_level):
|
def set_warning_filter(log_level):
|
||||||
@ -168,3 +178,21 @@ class LogConfigurator(object):
|
|||||||
self.file_logger.setFormatter(_FileFormatter(config=cloud_config))
|
self.file_logger.setFormatter(_FileFormatter(config=cloud_config))
|
||||||
self.file_logger.setLevel(log_level)
|
self.file_logger.setLevel(log_level)
|
||||||
self.root_logger.addHandler(self.file_logger)
|
self.root_logger.addHandler(self.file_logger)
|
||||||
|
|
||||||
|
logconfig = cloud_config.config.get('logging', None)
|
||||||
|
if logconfig:
|
||||||
|
highest_level = logging.NOTSET
|
||||||
|
for k in logconfig.keys():
|
||||||
|
level = log_level_from_string(logconfig[k])
|
||||||
|
logging.getLogger(k).setLevel(level)
|
||||||
|
if (highest_level < level):
|
||||||
|
highest_level = level
|
||||||
|
self.console_logger.setLevel(highest_level)
|
||||||
|
if self.file_logger:
|
||||||
|
self.file_logger.setLevel(highest_level)
|
||||||
|
# loggers that are not set will use the handler level, so we
|
||||||
|
# need to set the global level for all the loggers
|
||||||
|
for logkey in logging.Logger.manager.loggerDict.keys():
|
||||||
|
logger = logging.getLogger(logkey)
|
||||||
|
if logger.level == logging.NOTSET:
|
||||||
|
logger.setLevel(log_level)
|
||||||
|
Loading…
Reference in New Issue
Block a user