Skip to content

Commit b46c12b

Browse files
authored
Update GCF Python 3.7 backwards-compatible logging (#131)
* Fix logging monkeypatching * Add logging exception test
1 parent 60ac588 commit b46c12b

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

src/functions_framework/__init__.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import importlib.util
1717
import io
1818
import json
19+
import logging
1920
import os.path
2021
import pathlib
2122
import sys
@@ -62,6 +63,18 @@ def write(self, out):
6263
return self.stderr.write(json.dumps(payload) + "\n")
6364

6465

66+
def setup_logging():
67+
logging.getLogger().setLevel(logging.INFO)
68+
info_handler = logging.StreamHandler(sys.stdout)
69+
info_handler.setLevel(logging.NOTSET)
70+
info_handler.addFilter(lambda record: record.levelno <= logging.INFO)
71+
logging.getLogger().addHandler(info_handler)
72+
73+
warn_handler = logging.StreamHandler(sys.stderr)
74+
warn_handler.setLevel(logging.WARNING)
75+
logging.getLogger().addHandler(warn_handler)
76+
77+
6578
def _http_view_func_wrapper(function, request):
6679
def view_func(path):
6780
return function(request._get_current_object())
@@ -237,15 +250,9 @@ def handle_none(rv):
237250
app.make_response = handle_none
238251

239252
# Handle log severity backwards compatibility
240-
import logging # isort:skip
241-
242-
logging.info = _LoggingHandler("INFO", sys.stderr).write
243-
logging.warn = _LoggingHandler("ERROR", sys.stderr).write
244-
logging.warning = _LoggingHandler("ERROR", sys.stderr).write
245-
logging.error = _LoggingHandler("ERROR", sys.stderr).write
246-
logging.critical = _LoggingHandler("ERROR", sys.stderr).write
247253
sys.stdout = _LoggingHandler("INFO", sys.stderr)
248254
sys.stderr = _LoggingHandler("ERROR", sys.stderr)
255+
setup_logging()
249256

250257
# Extract the target function from the source file
251258
if not hasattr(source_module, target):

tests/test_functions.py

+16
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,22 @@ def test_legacy_function_log_severity(monkeypatch, capfd, mode, expected):
524524
assert expected in captured
525525

526526

527+
def test_legacy_function_log_exception(monkeypatch, capfd):
528+
source = TEST_FUNCTIONS_DIR / "http_log_exception" / "main.py"
529+
target = "function"
530+
severity = '"severity": "ERROR"'
531+
traceback = "Traceback (most recent call last)"
532+
533+
monkeypatch.setenv("ENTRY_POINT", target)
534+
535+
client = create_app(target, source).test_client()
536+
resp = client.post("/")
537+
captured = capfd.readouterr().err
538+
assert resp.status_code == 200
539+
assert severity in captured
540+
assert traceback in captured
541+
542+
527543
def test_legacy_function_returns_none(monkeypatch):
528544
source = TEST_FUNCTIONS_DIR / "returns_none" / "main.py"
529545
target = "function"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Function used in Worker tests of legacy GCF Python 3.7 logging."""
16+
import logging
17+
18+
X_GOOGLE_FUNCTION_NAME = "gcf-function"
19+
X_GOOGLE_ENTRY_POINT = "function"
20+
HOME = "/tmp"
21+
22+
23+
def function(request):
24+
"""Test function which logs exceptions.
25+
26+
Args:
27+
request: The HTTP request which triggered this function.
28+
"""
29+
try:
30+
raise Exception
31+
except:
32+
logging.exception("log")
33+
return None

0 commit comments

Comments
 (0)