]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Make neutron.common.log.log print module path
authorAkihiro MOTOKI <motoki@da.jp.nec.com>
Fri, 6 Sep 2013 17:10:57 +0000 (02:10 +0900)
committerAkihiro MOTOKI <motoki@da.jp.nec.com>
Fri, 6 Sep 2013 17:10:57 +0000 (02:10 +0900)
Closes-Bug: #1221663

neutron.common.log.log is useful for logging arguments of a method.
It outputs class name and method name, but module path is not output.
A module path is useful to search the log message.

Change-Id: Ic2903da750cc13980d5cdee153bb079f7d4ee122

neutron/common/log.py
neutron/tests/unit/test_common_log.py [new file with mode: 0644]

index fcc57b896ac892912138148b85d914e78a9a777b..85e4dce80d777508ab602de5aba6a559a0529794 100644 (file)
@@ -25,10 +25,11 @@ def log(method):
     """Decorator helping to log method calls."""
     def wrapper(*args, **kwargs):
         instance = args[0]
-        data = {"class_name": instance.__class__.__name__,
+        data = {"class_name": (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)
+                    ' called with arguments %(args)s %(kwargs)s'), data)
         return method(*args, **kwargs)
     return wrapper
diff --git a/neutron/tests/unit/test_common_log.py b/neutron/tests/unit/test_common_log.py
new file mode 100644 (file)
index 0000000..318613a
--- /dev/null
@@ -0,0 +1,70 @@
+# Copyright (c) 2013 OpenStack Foundation.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+import mock
+
+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
+    def test_method(self, arg1, arg2, *args, **kwargs):
+        pass
+
+
+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': {}}
+
+    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)
+
+    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)
+
+    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)
+
+    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)