From ce1baef7e74dcb793189f159b3440d7714745b1c Mon Sep 17 00:00:00 2001 From: mahendra Date: Tue, 22 Mar 2011 17:26:32 +0530 Subject: [PATCH] Replacing python logging with twisted log Python logging uses blocking APIs. Hence it must not be used in twisted code. A blocking log message can block the entire twisted event loop. Refer to: http://twistedmatrix.com/documents/10.2.0/core/howto/logging.html#auto3 --- paisley/client.py | 49 ++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/paisley/client.py b/paisley/client.py index bceb38a..3e253f3 100644 --- a/paisley/client.py +++ b/paisley/client.py @@ -16,7 +16,6 @@ import json import codecs -import logging import new from StringIO import StringIO @@ -167,25 +166,23 @@ def __init__(self, host, port=5984, dbName=None, self.bindToDB(dbName) if disable_log: - # since this is the db layer, and we generate a lot of logs, - # let people disable them completely if they want to. - levels = ['trace', 'debug', 'info', 'warn', 'error', 'exception'] class FakeLog(object): pass def nullfn(self, *a, **k): pass + self.log = FakeLog() - for level in levels: + for level in ['msg', 'err']: self.log.__dict__[level] = new.instancemethod(nullfn, self.log) - else: - self.log = logging.getLogger('paisley') + else: + self.log = log - self.log.debug("[%s%s:%s/%s] init new db client", - '%s@' % (username,) if username else '', - host, - port, - dbName if dbName else '') + self.log.msg("[%s%s:%s/%s] init new db client", + '%s@' % (username,) if username else '', + host, + port, + dbName if dbName else '') # If credentials are provided, prepare the auth header if username and password: @@ -220,15 +217,15 @@ def init_memcache( self ): def set_memcache( self, m_connect ): '''Invoked on a succesful connect to memcache''' - log.msg( 'Connected to memcache server' ) + self.log.msg( 'Connected to memcache server' ) self.mem_cn = m_connect def retry_memcache( self, error ): '''In case of an error in connecting to memcache, we will retry''' - log.err( error ) - log.msg( 'Retrying in %d seconds' % MEMCACHE_RETRY_INTERVAL ) + self.log.err( error ) + self.log.msg( 'Retrying in %d seconds' % MEMCACHE_RETRY_INTERVAL ) reactor.callLater( MEMCACHE_RETRY_INTERVAL, self.init_memcache ) @@ -242,13 +239,13 @@ def get_memc_data( self, key ): if self.memcache and self.mem_cn: try: if self.mem_cn._disconnected: - log.msg( 'Disconnected from memcache server' ) + self.log.msg( 'Disconnected from memcache server' ) yield self.init_memcache() fl, data = yield self.mem_cn.get( key ) except Exception, exp: data = None - log.err( 'Error GET(%s) : %s' % ( key, str(exp) ) ) + self.log.err( exp, 'Error GET(%s)' % key ) returnValue( data ) @@ -265,7 +262,7 @@ def set_memc_data( self, key, data, expireTime = 0 ): try: yield self.mem_cn.set( key, data, expireTime = expireTime ) except Exception, exp: - log.err( 'Error SET(%s) : %s' % ( key, str(exp) ) ) + self.log.err( exp, 'Error SET(%s)' % key ) def parseResult(self, result): @@ -555,8 +552,8 @@ def get(self, uri, descr=''): """ Execute a C{GET} at C{uri}. """ - self.log.debug("[%s:%s%s] GET %s", - self.host, self.port, short_print(uri), descr) + self.log.msg("[%s:%s%s] GET %s", + self.host, self.port, short_print(uri), descr) return self._getPage(uri, method="GET") data = yield self.get_memc_data(uri) @@ -573,8 +570,8 @@ def post(self, uri, body, descr=''): """ Execute a C{POST} of C{body} at C{uri}. """ - self.log.debug("[%s:%s%s] POST %s: %s", - self.host, self.port, short_print(uri), descr, short_print(repr(body))) + self.log.msg("[%s:%s%s] POST %s: %s", + self.host, self.port, short_print(uri), descr, short_print(repr(body))) return self._getPage(uri, method="POST", postdata=body) @@ -582,8 +579,8 @@ def put(self, uri, body, descr=''): """ Execute a C{PUT} of C{body} at C{uri}. """ - self.log.debug("[%s:%s%s] PUT %s: %s", - self.host, self.port, short_print(uri), descr, short_print(repr(body))) + self.log.msg("[%s:%s%s] PUT %s: %s", + self.host, self.port, short_print(uri), descr, short_print(repr(body))) return self._getPage(uri, method="PUT", postdata=body) @@ -591,6 +588,6 @@ def delete(self, uri, descr=''): """ Execute a C{DELETE} at C{uri}. """ - self.log.debug("[%s:%s%s] DELETE %s", - self.host, self.port, short_print(uri), descr) + self.log.msg("[%s:%s%s] DELETE %s", + self.host, self.port, short_print(uri), descr) return self._getPage(uri, method="DELETE")