-
Notifications
You must be signed in to change notification settings - Fork 7
/
logconfig.py
122 lines (103 loc) · 4.01 KB
/
logconfig.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
""" logconfig
This is essentially a template which can be copied into a python project and
used to easily achieve a good practice of logging. Modify the local copy as per
the project or site requirements.
"""
import os
import json
import logging.config
import logging.handlers
import threading
from dls_barcode import version
MAXBYTES = 1048576
BACKUPCOUNT = 20
ENCODING = "utf8"
default_config = {
"version": 1.0,
"disable_existing_loggers": False,
"formatters": {
"simple": {
"format": "%(message)s"
},
"extended": {
"format": "%(asctime)s - %(name)20s - %(levelname)6s - %(message)s"
},
"json": {
"format": "name: %(name)s, level: %(levelname)s, time: %(asctime)s, message: %(message)s"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"formatter": "simple",
"stream": "ext://sys.stdout"
},
"local_file_handler": {
"class": "logging.handlers.RotatingFileHandler",
# "class": "logging.handlers.FileHandler",
"level": "DEBUG",
"formatter": "extended",
"filename": "debug.log",
"maxBytes": MAXBYTES,
"backupCount": BACKUPCOUNT,
"encoding": ENCODING,
"delay" : True
},
},
"root": {
# Set the level here to be the default minimum level of log record to be produced
# If you set a handler to level DEBUG you will need to set either this level
"level": "DEBUG",
"handlers": ["local_file_handler"] #["console", "local_file_handler"]
}
}
class ThreadContextFilter(logging.Filter):
"""A logging context filter to add thread name and ID."""
def filter(self, record):
record.thread_id = str(threading.current_thread().ident)
record.thread_name = str(threading.current_thread().name)
return True
def setup_logging(
default_log_config=None,
default_level=logging.INFO,
env_key='LOG_CFG'
):
"""Setup logging configuration
Call this only once from the application main() function or __main__ module!
This will configure the python logging module based on a logging configuration
in the following order of priority:
1. Log configuration file found in the environment variable specified in the `env_key` argument.
2. Log configuration file found in the `default_log_config` argument.
3. Default log configuration found in the `logconfig.default_config` dict.
4. If all of the above fails: basicConfig is called with the `default_level` argument.
Args:
default_log_config (Optional[str]): Path to log configuration file.
env_key (Optional[str]): Environment variable that can optionally contain
a path to a configuration file.
default_level (int): logging level to set as default. Ignored if a log
configuration is found elsewhere.
Returns: None
"""
dict_config = None
logconfig_filename = default_log_config
env_var_value = os.getenv(env_key, None)
if env_var_value is not None:
logconfig_filename = env_var_value
if default_config is not None:
dict_config = default_config
if logconfig_filename is not None and os.path.exists(logconfig_filename):
with open(logconfig_filename, 'rt') as f:
file_config = json.load(f)
if file_config is not None:
dict_config = file_config
if dict_config is not None:
logging.config.dictConfig(dict_config)
else:
logging.basicConfig(level=default_level)
def set_additional_handler(file_name):
logger = logging.getLogger()
handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=MAXBYTES, backupCount=BACKUPCOUNT, encoding=ENCODING)
#json_format = default_config.get("formatters",{}).get("json",{}).get("format",{})
#handler.setFormatter(logging.Formatter(json_format))
logger.addHandler(handler)