-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging_manager.py
42 lines (36 loc) · 1.13 KB
/
logging_manager.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
"""
Used in the mrf_trainer file to backup stdout logging to a file for later inference.
"""
import logging
from contextlib import contextmanager
ROOT_LOGGER_NAME = 'mrf'
class LoggingMixin:
@property
def logger(self) -> logging.Logger:
name = '.'.join([ROOT_LOGGER_NAME, self.__module__, self.__class__.__name__])
return logging.getLogger(name)
@contextmanager
def setup_logging(debug: bool) -> logging.Logger:
logger = None
try:
default_logging_format = ''.join([
"[",
"%(asctime)s ",
"%(levelname)-8s ",
"%(filename)-15s:",
"%(lineno)3s - ",
"%(funcName)-10s ",
"] ",
"%(message)s",
])
logger = logging.getLogger('mrf')
logging.basicConfig(format=default_logging_format)
logger.setLevel(logging.DEBUG) if debug else logger.setLevel(logging.INFO)
yield logger
finally:
# __exit__
if logger:
handlers = logger.handlers[:]
for handler in handlers:
handler.close()
logger.removeHandler(handler)