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
1
+ import logging
26
2
27
3
28
4
class SQL (object ):
@@ -45,9 +21,6 @@ def __init__(self, url, **kwargs):
45
21
import sqlalchemy
46
22
import sqlite3
47
23
48
- # Get logger
49
- self ._logger = logging .getLogger ("cs50" )
50
-
51
24
# Require that file already exist for SQLite
52
25
matches = re .search (r"^sqlite:///(.+)$" , url )
53
26
if matches :
@@ -78,20 +51,17 @@ def connect(dbapi_connection, connection_record):
78
51
# Register listener
79
52
sqlalchemy .event .listen (self ._engine , "connect" , connect )
80
53
81
- # Log statements to standard error
82
- logging .basicConfig (level = logging .DEBUG )
83
-
84
54
# Test database
55
+ disabled = logging .root .disabled
56
+ logging .root .disabled = True
85
57
try :
86
- disabled = self ._logger .disabled
87
- self ._logger .disabled = True
88
58
self .execute ("SELECT 1" )
89
59
except sqlalchemy .exc .OperationalError as e :
90
60
e = RuntimeError (_parse_exception (e ))
91
61
e .__cause__ = None
92
62
raise e
93
63
finally :
94
- self . _logger .disabled = disabled
64
+ logging . root .disabled = disabled
95
65
96
66
def __del__ (self ):
97
67
"""Disconnect from database."""
@@ -103,7 +73,6 @@ def _disconnect(self):
103
73
self ._connection .close ()
104
74
delattr (self , "_connection" )
105
75
106
- @_enable_logging
107
76
def execute (self , sql , * args , ** kwargs ):
108
77
"""Execute a SQL statement."""
109
78
@@ -373,22 +342,22 @@ def shutdown_session(exception=None):
373
342
374
343
# If constraint violated, return None
375
344
except sqlalchemy .exc .IntegrityError as e :
376
- self . _logger .debug (termcolor .colored (statement , "yellow" ))
345
+ logging .debug (termcolor .colored (statement , "yellow" ))
377
346
e = ValueError (e .orig )
378
347
e .__cause__ = None
379
348
raise e
380
349
381
350
# If user error
382
351
except (sqlalchemy .exc .OperationalError , sqlalchemy .exc .ProgrammingError ) as e :
383
352
self ._disconnect ()
384
- self . _logger .debug (termcolor .colored (statement , "red" ))
353
+ logging .debug (termcolor .colored (statement , "red" ))
385
354
e = RuntimeError (e .orig )
386
355
e .__cause__ = None
387
356
raise e
388
357
389
358
# Return value
390
359
else :
391
- self . _logger .debug (termcolor .colored (_statement , "green" ))
360
+ logging .debug (termcolor .colored (_statement , "green" ))
392
361
return ret
393
362
394
363
def _escape (self , value ):
0 commit comments