[Sqlalchemy-commits] [1084] sqlalchemy/trunk/test: added util.Logger object with configurable thread
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-03-03 00:24:52
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:':';} #msg dl, #msg dt, #msg ul, #msg li { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[1084] sqlalchemy/trunk/test: added util.Logger object with configurable thread/timestamp view</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1084</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-03-02 18:24:41 -0600 (Thu, 02 Mar 2006)</dd> </dl> <h3>Log Message</h3> <pre>added util.Logger object with configurable thread/timestamp view</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemytrunklibsqlalchemyenginepy">sqlalchemy/trunk/lib/sqlalchemy/engine.py</a></li> <li><a href="#sqlalchemytrunklibsqlalchemypoolpy">sqlalchemy/trunk/lib/sqlalchemy/pool.py</a></li> <li><a href="#sqlalchemytrunklibsqlalchemyutilpy">sqlalchemy/trunk/lib/sqlalchemy/util.py</a></li> <li><a href="#sqlalchemytrunktestenginespy">sqlalchemy/trunk/test/engines.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemytrunklibsqlalchemyenginepy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/engine.py (1083 => 1084)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/engine.py 2006-03-02 17:38:37 UTC (rev 1083) +++ sqlalchemy/trunk/lib/sqlalchemy/engine.py 2006-03-03 00:24:41 UTC (rev 1084) </span><span class="lines">@@ -186,10 +186,7 @@ </span><span class="cx"> self.context = util.ThreadLocal(raiseerror=False) </span><span class="cx"> self._ischema = None </span><span class="cx"> self._figure_paramstyle() </span><del>- if logger is None: - self.logger = sys.stdout - else: - self.logger = logger </del><ins>+ self.logger = logger or util.Logger(origin='engine') </ins><span class="cx"> </span><span class="cx"> def _get_ischema(self): </span><span class="cx"> # We use a property for ischema so that the accessor </span><span class="lines">@@ -607,7 +604,7 @@ </span><span class="cx"> </span><span class="cx"> def log(self, msg): </span><span class="cx"> """logs a message using this SQLEngine's logger stream.""" </span><del>- self.logger.write(msg + "\n") </del><ins>+ self.logger.write(msg) </ins><span class="cx"> </span><span class="cx"> </span><span class="cx"> class ResultProxy: </span><span class="lines">@@ -685,7 +682,7 @@ </span><span class="cx"> """fetches one row, just like DBAPI cursor.fetchone().""" </span><span class="cx"> row = self.cursor.fetchone() </span><span class="cx"> if row is not None: </span><del>- if self.echo: print repr(row) </del><ins>+ if self.echo: self.engine.log(repr(row)) </ins><span class="cx"> return RowProxy(self, row) </span><span class="cx"> else: </span><span class="cx"> return None </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemypoolpy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/pool.py (1083 => 1084)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/pool.py 2006-03-02 17:38:37 UTC (rev 1083) +++ sqlalchemy/trunk/lib/sqlalchemy/pool.py 2006-03-03 00:24:41 UTC (rev 1084) </span><span class="lines">@@ -11,6 +11,7 @@ </span><span class="cx"> simply by calling regular DBAPI connect() methods.""" </span><span class="cx"> </span><span class="cx"> import Queue, weakref, string, cPickle </span><ins>+import util </ins><span class="cx"> </span><span class="cx"> try: </span><span class="cx"> import thread </span><span class="lines">@@ -69,10 +70,11 @@ </span><span class="cx"> </span><span class="cx"> </span><span class="cx"> class Pool(object): </span><del>- def __init__(self, echo = False, use_threadlocal = True): </del><ins>+ def __init__(self, echo = False, use_threadlocal = True, logger=None): </ins><span class="cx"> self._threadconns = weakref.WeakValueDictionary() </span><span class="cx"> self._use_threadlocal = use_threadlocal </span><span class="cx"> self._echo = echo </span><ins>+ self._logger = logger or util.Logger(origin='pool') </ins><span class="cx"> </span><span class="cx"> def connect(self): </span><span class="cx"> if not self._use_threadlocal: </span><span class="lines">@@ -115,7 +117,7 @@ </span><span class="cx"> raise NotImplementedError() </span><span class="cx"> </span><span class="cx"> def log(self, msg): </span><del>- print msg </del><ins>+ self.logger.write(msg) </ins><span class="cx"> </span><span class="cx"> class ConnectionFairy(object): </span><span class="cx"> def __init__(self, pool): </span></span></pre></div> <a id="sqlalchemytrunklibsqlalchemyutilpy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/util.py (1083 => 1084)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/util.py 2006-03-02 17:38:37 UTC (rev 1083) +++ sqlalchemy/trunk/lib/sqlalchemy/util.py 2006-03-03 00:24:41 UTC (rev 1084) </span><span class="lines">@@ -5,7 +5,7 @@ </span><span class="cx"> # the MIT License: http://www.opensource.org/licenses/mit-license.php </span><span class="cx"> </span><span class="cx"> __all__ = ['OrderedProperties', 'OrderedDict', 'generic_repr', 'HashSet', 'AttrProp'] </span><del>-import thread, weakref, UserList,string, inspect </del><ins>+import thread, threading, weakref, UserList, time, string, inspect, sys </ins><span class="cx"> from exceptions import * </span><span class="cx"> </span><span class="cx"> def to_list(x): </span><span class="lines">@@ -51,7 +51,34 @@ </span><span class="cx"> return obj.hash_key() </span><span class="cx"> else: </span><span class="cx"> return repr(obj) </span><del>- </del><ins>+ +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") + </ins><span class="cx"> class OrderedProperties(object): </span><span class="cx"> """ </span><span class="cx"> An object that maintains the order in which attributes are set upon it. </span></span></pre></div> <a id="sqlalchemytrunktestenginespy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/test/engines.py (1083 => 1084)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/test/engines.py 2006-03-02 17:38:37 UTC (rev 1083) +++ sqlalchemy/trunk/test/engines.py 2006-03-03 00:24:41 UTC (rev 1084) </span><span class="lines">@@ -121,7 +121,7 @@ </span><span class="cx"> table.insert().execute({'multi_id':3,'multi_rev':3,'name':'row3', 'value':'value3'}) </span><span class="cx"> table.select().execute().fetchall() </span><span class="cx"> table.drop() </span><del>- </del><ins>+ </ins><span class="cx"> def testtoengine(self): </span><span class="cx"> db = ansisql.engine() </span><span class="cx"> </span></span></pre> </div> </div> </body> </html> |