Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace python logger with twisted log #15

Open
wants to merge 1 commit into
base: d873c76e
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions paisley/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import json

import codecs
import logging
import new

from StringIO import StringIO
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 )


Expand All @@ -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 )

Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -573,24 +570,24 @@ 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)


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)


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")