self.context = util.ThreadLocal(raiseerror=False)
self._ischema = None
self._figure_paramstyle()
- if logger is None:
- self.logger = sys.stdout
- else:
- self.logger = logger
+ self.logger = logger or util.Logger(origin='engine')
def _get_ischema(self):
# We use a property for ischema so that the accessor
def log(self, msg):
"""logs a message using this SQLEngine's logger stream."""
- self.logger.write(msg + "\n")
+ self.logger.write(msg)
class ResultProxy:
"""fetches one row, just like DBAPI cursor.fetchone()."""
row = self.cursor.fetchone()
if row is not None:
- if self.echo: print repr(row)
+ if self.echo: self.engine.log(repr(row))
return RowProxy(self, row)
else:
return None
simply by calling regular DBAPI connect() methods."""
import Queue, weakref, string, cPickle
+import util
try:
import thread
class Pool(object):
- def __init__(self, echo = False, use_threadlocal = True):
+ def __init__(self, echo = False, use_threadlocal = True, logger=None):
self._threadconns = weakref.WeakValueDictionary()
self._use_threadlocal = use_threadlocal
self._echo = echo
+ self._logger = logger or util.Logger(origin='pool')
def connect(self):
if not self._use_threadlocal:
raise NotImplementedError()
def log(self, msg):
- print msg
+ self.logger.write(msg)
class ConnectionFairy(object):
def __init__(self, pool):
# the MIT License: http://www.opensource.org/licenses/mit-license.php
__all__ = ['OrderedProperties', 'OrderedDict', 'generic_repr', 'HashSet', 'AttrProp']
-import thread, weakref, UserList,string, inspect
+import thread, threading, weakref, UserList, time, string, inspect, sys
from exceptions import *
def to_list(x):
return obj.hash_key()
else:
return repr(obj)
-
+
+class Logger(object):
+ """defines various forms of logging"""
+ def __init__(self, logger=None, usethreads=False, usetimestamp=True, origin=None):
+ self.logger = logger or sys.stdout
+ self.usethreads = usethreads
+ self.usetimestamp = usetimestamp
+ self.origin = origin
+ def write(self, msg):
+ if self.usetimestamp:
+ t = time.time()
+ ms = (t - long(t)) * 1000
+ timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(t))
+ timestamp = "[%s,%03d]" % (timestamp, ms)
+ else:
+ timestamp = None
+ if self.origin:
+ origin = "[%s]" % self.origin
+ origin = "%-8s" % origin
+ else:
+ origin = None
+ if self.usethreads:
+ threadname = threading.currentThread().getName()
+ threadname = "[" + threadname + ' '*(8-len(threadname)) + "]"
+ else:
+ threadname = None
+ self.logger.write(string.join([s for s in (timestamp, threadname, origin) if s is not None]) + ": " + msg + "\n")
+
class OrderedProperties(object):
"""
An object that maintains the order in which attributes are set upon it.