Use method's logger in log decorator
Previously decorators shared logger. This patch makes LOG object in closure for every decorated method in order to use correct logging level. Closes-Bug: #1340778 Change-Id: If907acbab34f8bf23e1277e66c3ddd839f668764
This commit is contained in:
parent
b4c8114f79
commit
0a47197c6a
@ -15,21 +15,23 @@
|
||||
# under the License.
|
||||
|
||||
"""Log helper functions."""
|
||||
import functools
|
||||
|
||||
from neutron.openstack.common import log as logging
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def log(method):
|
||||
"""Decorator helping to log method calls."""
|
||||
LOG = logging.getLogger(method.__module__)
|
||||
|
||||
@functools.wraps(method)
|
||||
def wrapper(*args, **kwargs):
|
||||
instance = args[0]
|
||||
data = {"class_name": (instance.__class__.__module__ + '.'
|
||||
+ instance.__class__.__name__),
|
||||
data = {"class_name": "%s.%s" % (instance.__class__.__module__,
|
||||
instance.__class__.__name__),
|
||||
"method_name": method.__name__,
|
||||
"args": args[1:], "kwargs": kwargs}
|
||||
LOG.debug(_('%(class_name)s method %(method_name)s'
|
||||
' called with arguments %(args)s %(kwargs)s'), data)
|
||||
LOG.debug('%(class_name)s method %(method_name)s'
|
||||
' called with arguments %(args)s %(kwargs)s', data)
|
||||
return method(*args, **kwargs)
|
||||
return wrapper
|
||||
|
@ -18,9 +18,6 @@ from neutron.common import log as call_log
|
||||
from neutron.tests import base
|
||||
|
||||
|
||||
MODULE_NAME = 'neutron.tests.unit.test_common_log'
|
||||
|
||||
|
||||
class TargetKlass(object):
|
||||
|
||||
@call_log.log
|
||||
@ -32,39 +29,29 @@ class TestCallLog(base.BaseTestCase):
|
||||
def setUp(self):
|
||||
super(TestCallLog, self).setUp()
|
||||
self.klass = TargetKlass()
|
||||
self.expected_format = ('%(class_name)s method %(method_name)s '
|
||||
'called with arguments %(args)s %(kwargs)s')
|
||||
self.expected_data = {'class_name': MODULE_NAME + '.TargetKlass',
|
||||
'method_name': 'test_method',
|
||||
'args': (),
|
||||
'kwargs': {}}
|
||||
logger = self.klass.test_method.im_func.func_closure[0].cell_contents
|
||||
self.log_debug = mock.patch.object(logger, 'debug').start()
|
||||
|
||||
def _test_call_log(self, *args, **kwargs):
|
||||
expected_format = ('%(class_name)s method %(method_name)s '
|
||||
'called with arguments %(args)s %(kwargs)s')
|
||||
expected_data = {'class_name': '%s.%s' % (
|
||||
__name__,
|
||||
self.klass.__class__.__name__),
|
||||
'method_name': 'test_method',
|
||||
'args': args,
|
||||
'kwargs': kwargs}
|
||||
self.klass.test_method(*args, **kwargs)
|
||||
self.log_debug.assert_called_once_with(expected_format, expected_data)
|
||||
|
||||
def test_call_log_all_args(self):
|
||||
self.expected_data['args'] = (10, 20)
|
||||
with mock.patch.object(call_log.LOG, 'debug') as log_debug:
|
||||
self.klass.test_method(10, 20)
|
||||
log_debug.assert_called_once_with(self.expected_format,
|
||||
self.expected_data)
|
||||
self._test_call_log(10, 20)
|
||||
|
||||
def test_call_log_all_kwargs(self):
|
||||
self.expected_data['kwargs'] = {'arg1': 10, 'arg2': 20}
|
||||
with mock.patch.object(call_log.LOG, 'debug') as log_debug:
|
||||
self.klass.test_method(arg1=10, arg2=20)
|
||||
log_debug.assert_called_once_with(self.expected_format,
|
||||
self.expected_data)
|
||||
self._test_call_log(arg1=10, arg2=20)
|
||||
|
||||
def test_call_log_known_args_unknown_args_kwargs(self):
|
||||
self.expected_data['args'] = (10, 20, 30)
|
||||
self.expected_data['kwargs'] = {'arg4': 40}
|
||||
with mock.patch.object(call_log.LOG, 'debug') as log_debug:
|
||||
self.klass.test_method(10, 20, 30, arg4=40)
|
||||
log_debug.assert_called_once_with(self.expected_format,
|
||||
self.expected_data)
|
||||
self._test_call_log(10, 20, 30, arg4=40)
|
||||
|
||||
def test_call_log_known_args_kwargs_unknown_kwargs(self):
|
||||
self.expected_data['args'] = (10,)
|
||||
self.expected_data['kwargs'] = {'arg2': 20, 'arg3': 30, 'arg4': 40}
|
||||
with mock.patch.object(call_log.LOG, 'debug') as log_debug:
|
||||
self.klass.test_method(10, arg2=20, arg3=30, arg4=40)
|
||||
log_debug.assert_called_once_with(self.expected_format,
|
||||
self.expected_data)
|
||||
self._test_call_log(10, arg2=20, arg3=30, arg4=40)
|
||||
|
Loading…
Reference in New Issue
Block a user