forked from zewelor/bt-mqtt-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
52 lines (37 loc) · 1.28 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import logging
import logging.config
import yaml
APP_ROOT = "bt-mqtt-gw"
SUPPRESSION_ENABLED = False
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)