Skip to content

Commit fca53b9

Browse files
committed
fixed exception handling for Flask 1.0
1 parent 7a7dc37 commit fca53b9

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

src/cs50/flask.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import flask.logging
2+
13
from distutils.version import StrictVersion
24
from pkg_resources import get_distribution
35

@@ -6,28 +8,14 @@
68
# Try to monkey-patch Flask, if installed
79
try:
810

9-
# Only patch 0.12 (in case logging changes in 0.13)
11+
# Only patch >= 1.0
1012
version = StrictVersion(get_distribution("flask").version)
11-
assert version >= StrictVersion("0.10") and version < StrictVersion("0.13")
12-
13-
# Get default logger
14-
import flask.logging
15-
f = flask.logging.create_logger
16-
17-
def create_logger(app):
18-
"""Wrap default logger"""
19-
20-
# Create default logger
21-
logger = f(app)
22-
23-
# Reformat default logger's exceptions
24-
# https://docs.python.org/3/library/logging.html#logging.Formatter.formatException
25-
for handler in logger.handlers:
26-
handler.formatter.formatException = lambda exc_info: formatException(*exc_info)
27-
return logger
13+
assert version >= StrictVersion("1.0")
2814

29-
# Replace default logger
30-
flask.logging.create_logger = create_logger
15+
# Reformat logger's exceptions
16+
# http://flask.pocoo.org/docs/1.0/logging/
17+
# https://docs.python.org/3/library/logging.html#logging.Formatter.formatException
18+
flask.logging.default_handler.formatter.formatException = lambda exc_info: formatException(*exc_info)
3119

3220
except:
3321
pass

0 commit comments

Comments
 (0)