/
logging_utils.py
119 lines (96 loc) · 3.43 KB
/
logging_utils.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
import contextlib
import logging
import logging.config
import re
import sys
# Logging format example:
# 2018/11/20 12:36:37 INFO mlflow.sagemaker: Creating new SageMaker endpoint
LOGGING_LINE_FORMAT = "%(asctime)s %(levelname)s %(name)s: %(message)s"
LOGGING_DATETIME_FORMAT = "%Y/%m/%d %H:%M:%S"
class MlflowLoggingStream:
"""
A Python stream for use with event logging APIs throughout MLflow (`eprint()`,
`logger.info()`, etc.). This stream wraps `sys.stderr`, forwarding `write()` and
`flush()` calls to the stream referred to by `sys.stderr` at the time of the call.
It also provides capabilities for disabling the stream to silence event logs.
"""
def __init__(self):
self._enabled = True
def write(self, text):
if self._enabled:
sys.stderr.write(text)
def flush(self):
if self._enabled:
sys.stderr.flush()
@property
def enabled(self):
return self._enabled
@enabled.setter
def enabled(self, value):
self._enabled = value
MLFLOW_LOGGING_STREAM = MlflowLoggingStream()
def disable_logging():
"""
Disables the `MlflowLoggingStream` used by event logging APIs throughout MLflow
(`eprint()`, `logger.info()`, etc), silencing all subsequent event logs.
"""
MLFLOW_LOGGING_STREAM.enabled = False
def enable_logging():
"""
Enables the `MlflowLoggingStream` used by event logging APIs throughout MLflow
(`eprint()`, `logger.info()`, etc), emitting all subsequent event logs. This
reverses the effects of `disable_logging()`.
"""
MLFLOW_LOGGING_STREAM.enabled = True
def _configure_mlflow_loggers(root_module_name):
logging.config.dictConfig(
{
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"mlflow_formatter": {
"format": LOGGING_LINE_FORMAT,
"datefmt": LOGGING_DATETIME_FORMAT,
},
},
"handlers": {
"mlflow_handler": {
"formatter": "mlflow_formatter",
"class": "logging.StreamHandler",
"stream": MLFLOW_LOGGING_STREAM,
},
},
"loggers": {
root_module_name: {
"handlers": ["mlflow_handler"],
"level": "INFO",
"propagate": False,
},
},
}
)
def eprint(*args, **kwargs):
print(*args, file=MLFLOW_LOGGING_STREAM, **kwargs)
class LoggerMessageFilter(logging.Filter):
def __init__(self, module: str, filter_regex: re.Pattern):
super().__init__()
self._pattern = filter_regex
self._module = module
def filter(self, record):
if record.name == self._module and self._pattern.search(record.msg):
return False
return True
@contextlib.contextmanager
def suppress_logs(module: str, filter_regex: re.Pattern):
"""
Context manager that suppresses log messages from the specified module that match the specified
regular expression. This is useful for suppressing expected log messages from third-party
libraries that are not relevant to the current test.
"""
logger = logging.getLogger(module)
filter = LoggerMessageFilter(module=module, filter_regex=filter_regex)
logger.addFilter(filter)
try:
yield
finally:
logger.removeFilter(filter)