|
14 | 14 |
|
15 | 15 | import functools |
16 | 16 | import importlib.util |
| 17 | +import io |
17 | 18 | import json |
18 | 19 | import os.path |
19 | 20 | import pathlib |
@@ -65,6 +66,19 @@ def __init__( |
65 | 66 | self.data = data |
66 | 67 |
|
67 | 68 |
|
| 69 | +class _LoggingHandler(io.TextIOWrapper): |
| 70 | + """Logging replacement for stdout and stderr in GCF Python 3.7.""" |
| 71 | + |
| 72 | + def __init__(self, level, stderr=sys.stderr): |
| 73 | + io.TextIOWrapper.__init__(self, io.StringIO(), encoding=stderr.encoding) |
| 74 | + self.level = level |
| 75 | + self.stderr = stderr |
| 76 | + |
| 77 | + def write(self, out): |
| 78 | + payload = dict(severity=self.level, message=out.rstrip("\n")) |
| 79 | + return self.stderr.write(json.dumps(payload) + "\n") |
| 80 | + |
| 81 | + |
68 | 82 | def _http_view_func_wrapper(function, request): |
69 | 83 | def view_func(path): |
70 | 84 | return function(request._get_current_object()) |
@@ -221,6 +235,17 @@ def handle_none(rv): |
221 | 235 |
|
222 | 236 | app.make_response = handle_none |
223 | 237 |
|
| 238 | + # Handle log severity backwards compatibility |
| 239 | + import logging # isort:skip |
| 240 | + |
| 241 | + logging.info = _LoggingHandler("INFO", sys.stderr).write |
| 242 | + logging.warn = _LoggingHandler("ERROR", sys.stderr).write |
| 243 | + logging.warning = _LoggingHandler("ERROR", sys.stderr).write |
| 244 | + logging.error = _LoggingHandler("ERROR", sys.stderr).write |
| 245 | + logging.critical = _LoggingHandler("ERROR", sys.stderr).write |
| 246 | + sys.stdout = _LoggingHandler("INFO", sys.stderr) |
| 247 | + sys.stderr = _LoggingHandler("ERROR", sys.stderr) |
| 248 | + |
224 | 249 | # Extract the target function from the source file |
225 | 250 | if not hasattr(source_module, target): |
226 | 251 | raise MissingTargetException( |
|
0 commit comments