]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added util.Logger object with configurable thread/timestamp view
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 3 Mar 2006 00:24:41 +0000 (00:24 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 3 Mar 2006 00:24:41 +0000 (00:24 +0000)
lib/sqlalchemy/engine.py
lib/sqlalchemy/pool.py
lib/sqlalchemy/util.py
test/engines.py

index 757d3517eed22b77b42ccb59f1ac139c451ee655..8828e2a0bd7c6e7a3efebd4a3735d02321363cfe 100644 (file)
@@ -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
index fe5c29e88eb4487873c55265834214c76bbde71c..783b4450bf943300f4b2d9d6d98b914250b6bb15 100644 (file)
@@ -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):
index 618900fcfa8231f09fa1668795196550b0f1d5ec..02bd5d587c57f039ba13b8b30aa992bbf8514838 100644 (file)
@@ -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.
index aeb962c9b3e59da6112927cc12a0edf055d4dba7..3bb20c7251cd8b5c9f46db798003919762c8e6e2 100644 (file)
@@ -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()