Skip to content

Commit

Permalink
Fixed up logging to separate api calls from internal stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eli Stevens committed Nov 18, 2011
1 parent 9c60c63 commit e589265
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions couchable/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@

# logging
import logging
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
log_api = logging.getLogger(__name__.replace('core', 'api'))
log_api.setLevel(logging.WARN)

print __name__
log_internal = logging.getLogger(__name__.replace('core', 'internal'))
log_internal.setLevel(logging.WARN)


"""
Expand Down Expand Up @@ -229,7 +230,7 @@ def __init__(self, url=None, name=None, db=None):
try:
db = self.server[self.name]
except:
log.warn("Creating DB: {}".format(self.url))
log_api.warn("Creating DB: {}".format(self.url))
db = self.server.create(self.name)

self.db = db
Expand Down Expand Up @@ -347,8 +348,6 @@ def store(self, what, skip=None):
@rtype: str or list
@return: The C{._id} of the C{what} parameter, or the list of such IDs if C{what} was a list.
"""
log.info('CouchableDb.store(what={!r}, skip={!r})'.format(what, skip))

if skip is None:
self._skip_list = []
else:
Expand All @@ -359,6 +358,11 @@ def store(self, what, skip=None):
else:
store_list = what

if len(store_list) > 3:
log_api.info('CouchableDb.store(what={!r}, skip={!r})'.format(store_list[:3] + ['...'], skip))
else:
log_api.info('CouchableDb.store(what={!r}, skip={!r})'.format(what, skip))

self._done_dict = collections.OrderedDict()
self._cycle_set = set()

Expand Down Expand Up @@ -433,7 +437,7 @@ def store(self, what, skip=None):
#print ret_list
for (success, _id, _rev), (obj, doc) in itertools.izip(ret_list, bulk_list):
if not success:
log.warn("Error updating {} {}, {}: ".format(type(obj), _id, _rev) + repr(doc))
log_internal.warn("Error updating {} {}, {}: ".format(type(obj), _id, _rev) + repr(doc))
raise _rev
else:
obj._rev = _rev
Expand Down Expand Up @@ -490,9 +494,9 @@ def _pack(self, parent_doc, data, attachment_dict, name, isKey=False):
#print ''.join(traceback.format_stack())
return handler(self, parent_doc, data, attachment_dict, name, isKey)
except RuntimeError:
log.error(name)
log_internal.error(name)
except Exception, e:
log.error(name)
log_internal.error(name)
raise

#if handler:
Expand Down Expand Up @@ -1080,8 +1084,6 @@ def load(self, what, loaded=None):
@rtype: obj or list
@return: The object indicated by the C{what} parameter, or a list of such objects if C{what} was a list.
"""
log.info('CouchableDb.load(what={!r}, loaded={!r})'.format(what, loaded))

id_list = []

if isinstance(loaded, list):
Expand All @@ -1095,6 +1097,12 @@ def load(self, what, loaded=None):
else:
load_list = what

if len(load_list) > 3:
log_api.info('CouchableDb.load(what={!r}, loaded={!r})'.format(load_list[:3] + ['...'], loaded))
else:
log_api.info('CouchableDb.load(what={!r}, loaded={!r})'.format(what, loaded))


for item in load_list:
#print "item", item
if isinstance(item, basestring):
Expand Down Expand Up @@ -1134,25 +1142,16 @@ def load(self, what, loaded=None):

def _load(self, _id, loaded_dict):
if _id not in loaded_dict:
#try:
#print datetime.datetime.now(), "690: self.db[_id]"
loaded_dict[_id] = self.db[_id]
#except:
# print "problem:", _id
# raise
log_internal.info("Fetching object from DB: {}".format(_id))
loaded_dict[_id] = self.db[_id]

doc = loaded_dict[_id]

#print _id, doc, loaded_dict

obj = self._obj_by_id.get(_id, None)
if obj is None or getattr(obj, '_rev', None) != doc['_rev']:
#print obj is None or getattr(obj, '_id', 'no id'), obj is None or getattr(obj, '_rev', 'no rev'), doc['_rev']
#print self._obj_by_id.items()

#if 'pickles' in doc[FIELD_NAME]:
# doc[FIELD_NAME]['pickles'] = pickle.loads(doc[FIELD_NAME]['pickles'])

log_internal.info("Unpacking object: {}".format(_id))
obj = self._unpack(doc, doc, loaded_dict, obj)

base_cls, func_tuple = findHandler(type(obj), _couchable_types)
Expand All @@ -1162,7 +1161,7 @@ def _load(self, _id, loaded_dict):
try:
obj._cdb = self
except:
print obj
log_internal.exception('Cannot set obj._cdb to self, obj: {!r}'.format(obj))
raise

return obj
Expand Down

0 comments on commit e589265

Please sign in to comment.