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.
|
# under the License.
|
||||||
|
|
||||||
"""Log helper functions."""
|
"""Log helper functions."""
|
||||||
|
import functools
|
||||||
|
|
||||||
from neutron.openstack.common import log as logging
|
from neutron.openstack.common import log as logging
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
def log(method):
|
def log(method):
|
||||||
"""Decorator helping to log method calls."""
|
"""Decorator helping to log method calls."""
|
||||||
|
LOG = logging.getLogger(method.__module__)
|
||||||
|
|
||||||
|
@functools.wraps(method)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
instance = args[0]
|
instance = args[0]
|
||||||
data = {"class_name": (instance.__class__.__module__ + '.'
|
data = {"class_name": "%s.%s" % (instance.__class__.__module__,
|
||||||
+ instance.__class__.__name__),
|
instance.__class__.__name__),
|
||||||
"method_name": method.__name__,
|
"method_name": method.__name__,
|
||||||
"args": args[1:], "kwargs": kwargs}
|
"args": args[1:], "kwargs": kwargs}
|
||||||
LOG.debug(_('%(class_name)s method %(method_name)s'
|
LOG.debug('%(class_name)s method %(method_name)s'
|
||||||
' called with arguments %(args)s %(kwargs)s'), data)
|
' called with arguments %(args)s %(kwargs)s', data)
|
||||||
return method(*args, **kwargs)
|
return method(*args, **kwargs)
|
||||||
return wrapper
|
return wrapper
|
||||||
|
@ -18,9 +18,6 @@ from neutron.common import log as call_log
|
|||||||
from neutron.tests import base
|
from neutron.tests import base
|
||||||
|
|
||||||
|
|
||||||
MODULE_NAME = 'neutron.tests.unit.test_common_log'
|
|
||||||
|
|
||||||
|
|
||||||
class TargetKlass(object):
|
class TargetKlass(object):
|
||||||
|
|
||||||
@call_log.log
|
@call_log.log
|
||||||
@ -32,39 +29,29 @@ class TestCallLog(base.BaseTestCase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestCallLog, self).setUp()
|
super(TestCallLog, self).setUp()
|
||||||
self.klass = TargetKlass()
|
self.klass = TargetKlass()
|
||||||
self.expected_format = ('%(class_name)s method %(method_name)s '
|
logger = self.klass.test_method.im_func.func_closure[0].cell_contents
|
||||||
'called with arguments %(args)s %(kwargs)s')
|
self.log_debug = mock.patch.object(logger, 'debug').start()
|
||||||
self.expected_data = {'class_name': MODULE_NAME + '.TargetKlass',
|
|
||||||
'method_name': 'test_method',
|
def _test_call_log(self, *args, **kwargs):
|
||||||
'args': (),
|
expected_format = ('%(class_name)s method %(method_name)s '
|
||||||
'kwargs': {}}
|
'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):
|
def test_call_log_all_args(self):
|
||||||
self.expected_data['args'] = (10, 20)
|
self._test_call_log(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)
|
|
||||||
|
|
||||||
def test_call_log_all_kwargs(self):
|
def test_call_log_all_kwargs(self):
|
||||||
self.expected_data['kwargs'] = {'arg1': 10, 'arg2': 20}
|
self._test_call_log(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)
|
|
||||||
|
|
||||||
def test_call_log_known_args_unknown_args_kwargs(self):
|
def test_call_log_known_args_unknown_args_kwargs(self):
|
||||||
self.expected_data['args'] = (10, 20, 30)
|
self._test_call_log(10, 20, 30, arg4=40)
|
||||||
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)
|
|
||||||
|
|
||||||
def test_call_log_known_args_kwargs_unknown_kwargs(self):
|
def test_call_log_known_args_kwargs_unknown_kwargs(self):
|
||||||
self.expected_data['args'] = (10,)
|
self._test_call_log(10, arg2=20, arg3=30, arg4=40)
|
||||||
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)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user