Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2cc2f0c

Browse files
dmalanKareem Zidane
authored andcommittedFeb 20, 2019
enabling logging for Flask
1 parent 9b0d72d commit 2cc2f0c

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed
 

‎src/cs50/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import logging
21
import os
32
import sys
43

54
try:
65

76
# Save student's sys.path
8-
path = sys.path[:]
7+
_path = sys.path[:]
98

109
# In case student has files that shadow packages
1110
sys.path = [p for p in sys.path if p not in ("", os.getcwd())]
1211

13-
# Disable logger by default
14-
#logging.getLogger("cs50").disabled = True
15-
1612
# Import cs50_*
1713
from .cs50 import get_char, get_float, get_int, get_string
1814
try:
@@ -29,4 +25,4 @@
2925
finally:
3026

3127
# Restore student's sys.path (just in case library raised an exception that caller caught)
32-
sys.path = path
28+
sys.path = _path

‎src/cs50/flask.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
from distutils.version import StrictVersion
24
from os import getenv
35
from pkg_resources import get_distribution
@@ -8,8 +10,8 @@
810
try:
911

1012
# Only patch >= 1.0
11-
version = StrictVersion(get_distribution("flask").version)
12-
assert version >= StrictVersion("1.0")
13+
_version = StrictVersion(get_distribution("flask").version)
14+
assert _version >= StrictVersion("1.0")
1315

1416
# Reformat logger's exceptions
1517
# http://flask.pocoo.org/docs/1.0/logging/
@@ -20,19 +22,42 @@
2022
except:
2123
pass
2224

25+
# Enable logging when Flask is in use,
26+
# monkey-patching own SQL module, which shouldn't need to know about Flask
27+
logging.getLogger("cs50").disabled = True
28+
try:
29+
import flask
30+
from .sql import SQL
31+
_before = SQL.execute
32+
def _after(*args, **kwargs):
33+
disabled = logging.getLogger("cs50").disabled
34+
if flask.current_app:
35+
logging.getLogger("cs50").disabled = False
36+
try:
37+
_before(*args, **kwargs)
38+
except:
39+
logging.getLogger("cs50").disabled = disabled
40+
raise
41+
else:
42+
logging.getLogger("cs50").disabled = disabled
43+
SQL.execute = _after
44+
except:
45+
pass
46+
2347
# Add support for Cloud9 proxy so that flask.redirect doesn't redirect from HTTPS to HTTP
2448
# http://stackoverflow.com/a/23504684/5156190
2549
if getenv("C9_HOSTNAME") and not getenv("IDE_OFFLINE"):
2650
try:
2751
import flask
2852
from werkzeug.contrib.fixers import ProxyFix
29-
before = flask.Flask.__init__
30-
def after(self, *args, **kwargs):
31-
before(self, *args, **kwargs)
53+
_before = flask.Flask.__init__
54+
def _after(*args, **kwargs):
55+
_before(*args, **kwargs)
3256
self.wsgi_app = ProxyFix(self.wsgi_app)
33-
flask.Flask.__init__ = after
57+
flask.Flask.__init__ = _after
3458
except:
3559
pass
3660

37-
except:
61+
except Exception as e:
62+
print(e)
3863
pass

‎src/cs50/sql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ def connect(dbapi_connection, connection_record):
6464
disabled = self._logger.disabled
6565
self._logger.disabled = True
6666
self.execute("SELECT 1")
67+
self._logger.disabled = disabled
6768
except sqlalchemy.exc.OperationalError as e:
6869
e = RuntimeError(self._parse_exception(e))
6970
e.__cause__ = None
70-
raise e
71-
else:
7271
self._logger.disabled = disabled
72+
raise e
7373

7474
def execute(self, sql, *args, **kwargs):
7575
"""Execute a SQL statement."""

‎tests/flask/application.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
sys.path.insert(0, "../../src")
66

77
import cs50
8+
import cs50.flask
89

910
app = Flask(__name__)
1011

12+
db = cs50.SQL("sqlite:///../sqlite.db")
13+
1114
@app.route("/")
1215
def index():
16+
db.execute("SELECT 1")
17+
"""
1318
def f():
1419
res = requests.get("cs50.harvard.edu")
1520
f()
21+
"""
1622
return render_template("index.html")

‎tests/sqlite.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import logging
12
import sys
23

34
sys.path.insert(0, "../src")
45

56
from cs50 import SQL
67

8+
logging.getLogger("cs50").disabled = False
9+
710
db = SQL("sqlite:///sqlite.db")
811
db.execute("SELECT 1")
912

0 commit comments

Comments
 (0)
Please sign in to comment.