Skip to content

Commit 7dd19ad

Browse files
author
Kareem Zidane
committed
forcibly enable cs50 logger in flask apps
1 parent f2ba330 commit 7dd19ad

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/cs50/sql.py

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
def _enable_logging(f):
2+
"""Enable logging of SQL statements when Flask is in use."""
3+
4+
import logging
5+
import functools
6+
7+
@functools.wraps(f)
8+
def decorator(*args, **kwargs):
9+
10+
# Infer whether Flask is installed
11+
try:
12+
import flask
13+
except ModuleNotFoundError:
14+
return f(*args, **kwargs)
15+
16+
# Enable logging
17+
disabled = logging.getLogger("cs50").disabled
18+
if flask.current_app:
19+
logging.getLogger("cs50").disabled = False
20+
try:
21+
return f(*args, **kwargs)
22+
finally:
23+
logging.getLogger("cs50").disabled = disabled
24+
25+
return decorator
26+
27+
128
class SQL(object):
229
"""Wrap SQLAlchemy to provide a simple SQL API."""
330

@@ -73,6 +100,7 @@ def _disconnect(self):
73100
self._connection.close()
74101
delattr(self, "_connection")
75102

103+
@_enable_logging
76104
def execute(self, sql, *args, **kwargs):
77105
"""Execute a SQL statement."""
78106

0 commit comments

Comments
 (0)