Improved logging mechanism (and a few minor goodies) (#54)
* Improved logging mechanism * Allow for suppression of any errors regarding failed device updates
This commit is contained in:
@@ -1,6 +1,50 @@
|
||||
import logging
|
||||
import logging.config
|
||||
import yaml
|
||||
|
||||
import sys
|
||||
APP_ROOT = 'bt-mqtt-gw'
|
||||
SUPPRESSION_ENABLED = False
|
||||
|
||||
_LOGGER = logging.getLogger('bt-mqtt-gw')
|
||||
_LOGGER.addHandler(logging.StreamHandler(sys.stdout))
|
||||
|
||||
def setup():
|
||||
with open('logger.yaml', 'rt') as f:
|
||||
config = yaml.safe_load(f.read())
|
||||
logging.config.dictConfig(config)
|
||||
|
||||
|
||||
def get(name=None):
|
||||
if name:
|
||||
logger_name = '{}.{}'.format(APP_ROOT, name)
|
||||
else:
|
||||
logger_name = APP_ROOT
|
||||
return logging.getLogger(logger_name)
|
||||
|
||||
|
||||
def enable_debug_formatter():
|
||||
logging.getLogger().handlers[0].setFormatter(logging.getLogger('dummy_debug').handlers[0].formatter)
|
||||
|
||||
|
||||
def reset():
|
||||
app_level = get().getEffectiveLevel()
|
||||
|
||||
root = logging.getLogger()
|
||||
map(root.removeHandler, root.handlers[:])
|
||||
map(root.removeFilter, root.filters[:])
|
||||
|
||||
setup()
|
||||
get().setLevel(app_level)
|
||||
if app_level <= logging.DEBUG:
|
||||
enable_debug_formatter()
|
||||
|
||||
|
||||
def suppress_update_failures(suppress):
|
||||
global SUPPRESSION_ENABLED
|
||||
SUPPRESSION_ENABLED = suppress
|
||||
|
||||
|
||||
def log_exception(logger, message, *args, **kwargs):
|
||||
if not ('suppress' in kwargs and kwargs.pop('suppress') and SUPPRESSION_ENABLED):
|
||||
if logger.isEnabledFor(logging.DEBUG):
|
||||
logger.exception(message, *args, **kwargs)
|
||||
elif logger.isEnabledFor(logging.WARNING):
|
||||
logger.warning(message, *args, **kwargs)
|
||||
|
||||
Reference in New Issue
Block a user