From 13710ae742c18eb589a204bc9d242cccf66ae4b0 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 3 Mar 2006 00:24:41 +0000 Subject: [PATCH] added util.Logger object with configurable thread/timestamp view --- lib/sqlalchemy/engine.py | 9 +++------ lib/sqlalchemy/pool.py | 6 ++++-- lib/sqlalchemy/util.py | 31 +++++++++++++++++++++++++++++-- test/engines.py | 2 +- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/lib/sqlalchemy/engine.py b/lib/sqlalchemy/engine.py index 757d3517ee..8828e2a0bd 100644 --- a/lib/sqlalchemy/engine.py +++ b/lib/sqlalchemy/engine.py @@ -186,10 +186,7 @@ class SQLEngine(schema.SchemaEngine): 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 @@ -607,7 +604,7 @@ class SQLEngine(schema.SchemaEngine): def log(self, msg): """logs a message using this SQLEngine's logger stream.""" - self.logger.write(msg + "\n") + self.logger.write(msg) class ResultProxy: @@ -685,7 +682,7 @@ 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 diff --git a/lib/sqlalchemy/pool.py b/lib/sqlalchemy/pool.py index fe5c29e88e..783b4450bf 100644 --- a/lib/sqlalchemy/pool.py +++ b/lib/sqlalchemy/pool.py @@ -11,6 +11,7 @@ be managed automatically, based on module type and connect arguments, simply by calling regular DBAPI connect() methods.""" import Queue, weakref, string, cPickle +import util try: import thread @@ -69,10 +70,11 @@ def clear_managers(): 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: @@ -115,7 +117,7 @@ class Pool(object): raise NotImplementedError() def log(self, msg): - print msg + self.logger.write(msg) class ConnectionFairy(object): def __init__(self, pool): diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index 618900fcfa..02bd5d587c 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -5,7 +5,7 @@ # 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): @@ -51,7 +51,34 @@ def hash_key(obj): 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. diff --git a/test/engines.py b/test/engines.py index aeb962c9b3..3bb20c7251 100644 --- a/test/engines.py +++ b/test/engines.py @@ -121,7 +121,7 @@ class EngineTest(PersistTest): table.insert().execute({'multi_id':3,'multi_rev':3,'name':'row3', 'value':'value3'}) table.select().execute().fetchall() table.drop() - + def testtoengine(self): db = ansisql.engine() -- 2.47.2