Browse files

Some context and mediator logging.

  • Loading branch information...
1 parent 3c99320 commit 5e0756a805e0af69e243deecda67028774a738ea @kgaughan committed Oct 11, 2012
Showing with 21 additions and 0 deletions.
  1. +21 −0 dbkit.py
View
21 dbkit.py
@@ -12,6 +12,7 @@
import contextlib
import datetime
import functools
+import logging
import pprint
import sys
import threading
@@ -48,6 +49,10 @@
'NotSupportedError')
+# Use for in-library logging (as opposed to tracing statement logging).
+logger = logging.getLogger(__name__)
+
+
class NoContext(StandardError):
"""You are attempting to use dbkit outside of a database context."""
__slots__ = ()
@@ -131,6 +136,10 @@ def transaction(self):
Sets up a context where all the statements within it are ran within
a single database transaction. For internal use only.
"""
+ if self._depth == 0:
+ logger.debug("Starting transaction")
+ else:
+ logger.debug("Starting fake subtransaction")
# The idea here is to fake the nesting of transactions. Only when
# we've gotten back to the topmost transaction context do we actually
# commit or rollback.
@@ -156,23 +165,28 @@ def transaction(self):
def cursor(self):
"""Get a cursor for the current connection. For internal use only."""
with self._mdr:
+ logger.debug("Creating cursor")
cursor = self._mdr.cursor()
try:
+ logger.debug("Yielding cursor")
yield cursor
if cursor.rowcount != -1:
self.last_row_count = cursor.rowcount
self.last_row_id = getattr(cursor, 'lastrowid', None)
except:
self.last_row_count = None
self.last_row_id = None
+ logger.debug("Closing cursor")
cursor.close()
raise
def execute(self, stmt, args):
"""Execute a statement, returning a cursor. For internal use only."""
self.logger(stmt, args)
with self.cursor() as cursor:
+ logger.debug("Executing statement")
cursor.execute(stmt, args)
+ logger.debug("Statement successfully executed")
return cursor
def execute_proc(self, procname, args):
@@ -182,11 +196,14 @@ def execute_proc(self, procname, args):
"""
self.logger(procname, args)
with self.cursor() as cursor:
+ logger.debug("Executing stored procedure")
cursor.callproc(procname, args)
+ logger.debug("Stored procedure successfully executed")
return cursor
def close(self):
"""Close the connection this context wraps."""
+ logger.debug("Closing context")
self.logger = None
for exc in _EXCEPTIONS:
setattr(self, exc, None)
@@ -237,11 +254,15 @@ def close(self):
def rollback(self):
"""Rollback the current transaction."""
+ logger.debug("Rolling back transaction")
self.conn.rollback()
+ logger.debug("Transaction rolled back")
def commit(self):
"""Commit the current transaction."""
+ logger.debug("Committing transaction")
self.conn.commit()
+ logger.debug("Transaction committed")
class SingleConnectionMediator(ConnectionMediatorBase):

0 comments on commit 5e0756a

Please sign in to comment.