From bd2ea9e6aaec69462b6aecd77b275a68dbf2e690 Mon Sep 17 00:00:00 2001 From: bdzim Date: Wed, 21 May 2014 16:01:49 -0400 Subject: [PATCH] added comments and now we log tracebacks --- netkes/account_mgr/accounts_api.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/netkes/account_mgr/accounts_api.py b/netkes/account_mgr/accounts_api.py index 0e4cfc7..489ab2e 100644 --- a/netkes/account_mgr/accounts_api.py +++ b/netkes/account_mgr/accounts_api.py @@ -11,28 +11,32 @@ class Error(Exception): pass -class ApiMeta(type): +class LogErrorMeta(type): + '''Decorate all functions so that they log all Errors.''' def __new__(cls, name, bases, attrs): for attr_name, attr_value in attrs.iteritems(): if isinstance(attr_value, types.FunctionType): attrs[attr_name] = cls.log_exceptions(attr_value) - return super(ApiMeta, cls).__new__(cls, name, bases, attrs) + return super(LogErrorMeta, cls).__new__(cls, name, bases, attrs) @classmethod def log_exceptions(cls, func): + '''Log the name and arguments of any function that raises an Error. + Then raise the exception. + ''' def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except Error: log = logging.getLogger('accounts_api') - log.error('%s - %s - %s' % (func.__name__, args, kwargs)) + log.exception('%s - %s - %s' % (func.__name__, args, kwargs)) raise return wrapper class Api(object): - __metaclass__ = ApiMeta + __metaclass__ = LogErrorMeta class BadParams(Error): pass