This repository was archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlogging.py
More file actions
52 lines (44 loc) · 1.28 KB
/
logging.py
File metadata and controls
52 lines (44 loc) · 1.28 KB
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
"""Common logging functions"""
import logging
import os
from logging.config import dictConfig
from typing import Optional
import yaml
LOGGING_CONF = 'logging.yaml'
LOGGING_DEFAULT = {
'version': 1,
'formatters': {
'default': {
'format': '%(asctime)s %(name)s %(levelname)s %(message)s'
}
},
'handlers': {
'default': {
'class': 'logging.StreamHandler',
'formatter': 'default'
}
},
'root': {
'handlers': ['default'],
'level': 'INFO'
}
}
def configure_logging(debug: Optional[bool] = False,
config: Optional[dict] = None,
filename: Optional[str] = LOGGING_CONF) -> logging.Logger:
"""Configure logging"""
if config is not None:
config_dict = config
config_source = 'dictionary'
elif filename is not None and os.path.exists(filename):
with open(filename, "rt") as file:
config_dict = yaml.safe_load(file)
config_source = 'file'
else:
config_dict = LOGGING_DEFAULT
config_source = 'default'
if debug:
config_dict['root']['level'] = 'DEBUG'
dictConfig(config_dict)
logging.debug("Configured logging using %s", config_source)
return logging.getLogger()