Merge "Use oslo_log.helpers.log_method_call"

This commit is contained in:
Jenkins 2015-07-18 00:53:47 +00:00 committed by Gerrit Code Review
commit 486c0ee522
5 changed files with 8 additions and 81 deletions

View File

@ -13,26 +13,11 @@
# under the License. # under the License.
"""Log helper functions.""" """Log helper functions."""
import functools
from oslo_log import log as logging from oslo_log import helpers
from oslo_log import versionutils from oslo_log import versionutils
@versionutils.deprecated(as_of=versionutils.deprecated.LIBERTY, log = versionutils.deprecated(
in_favor_of='oslo_log.helpers.log_method_call') as_of=versionutils.deprecated.LIBERTY,
def log(method): in_favor_of='oslo_log.helpers.log_method_call')(helpers.log_method_call)
"""Decorator helping to log method calls."""
LOG = logging.getLogger(method.__module__)
@functools.wraps(method)
def wrapper(*args, **kwargs):
instance = args[0]
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)
return method(*args, **kwargs)
return wrapper

View File

@ -17,6 +17,7 @@ from eventlet import greenthread
from oslo_config import cfg from oslo_config import cfg
from oslo_db import api as oslo_db_api from oslo_db import api as oslo_db_api
from oslo_db import exception as os_db_exception from oslo_db import exception as os_db_exception
from oslo_log import helpers as log_helpers
from oslo_log import log from oslo_log import log
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from oslo_utils import excutils from oslo_utils import excutils
@ -39,7 +40,6 @@ from neutron.callbacks import resources
from neutron.common import constants as const from neutron.common import constants as const
from neutron.common import exceptions as exc from neutron.common import exceptions as exc
from neutron.common import ipv6_utils from neutron.common import ipv6_utils
from neutron.common import log as neutron_log
from neutron.common import rpc as n_rpc from neutron.common import rpc as n_rpc
from neutron.common import topics from neutron.common import topics
from neutron.common import utils from neutron.common import utils
@ -162,7 +162,7 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
) )
self.start_periodic_dhcp_agent_status_check() self.start_periodic_dhcp_agent_status_check()
@neutron_log.log @log_helpers.log_method_call
def start_rpc_listeners(self): def start_rpc_listeners(self):
"""Start the RPC loop to let the plugin communicate with agents.""" """Start the RPC loop to let the plugin communicate with agents."""
self.topic = topics.PLUGIN self.topic = topics.PLUGIN

View File

@ -14,12 +14,12 @@
# under the License. # under the License.
from oslo_config import cfg from oslo_config import cfg
from oslo_log import helpers as log_helpers
from oslo_utils import importutils from oslo_utils import importutils
from neutron.api.rpc.agentnotifiers import l3_rpc_agent_api from neutron.api.rpc.agentnotifiers import l3_rpc_agent_api
from neutron.api.rpc.handlers import l3_rpc from neutron.api.rpc.handlers import l3_rpc
from neutron.common import constants as n_const from neutron.common import constants as n_const
from neutron.common import log as neutron_log
from neutron.common import rpc as n_rpc from neutron.common import rpc as n_rpc
from neutron.common import topics from neutron.common import topics
from neutron.db import common_db_mixin from neutron.db import common_db_mixin
@ -62,7 +62,7 @@ class L3RouterPlugin(common_db_mixin.CommonDbMixin,
l3_dvrscheduler_db.subscribe() l3_dvrscheduler_db.subscribe()
l3_db.subscribe() l3_db.subscribe()
@neutron_log.log @log_helpers.log_method_call
def setup_rpc(self): def setup_rpc(self):
# RPC support # RPC support
self.topic = topics.L3PLUGIN self.topic = topics.L3PLUGIN

View File

@ -1,57 +0,0 @@
# 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
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()
logger = self.klass.test_method.__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._test_call_log(10, 20)
def test_call_log_all_kwargs(self):
self._test_call_log(arg1=10, arg2=20)
def test_call_log_known_args_unknown_args_kwargs(self):
self._test_call_log(10, 20, 30, arg4=40)
def test_call_log_known_args_kwargs_unknown_kwargs(self):
self._test_call_log(10, arg2=20, arg3=30, arg4=40)

View File

@ -222,7 +222,6 @@ commands = python -m testtools.run \
neutron.tests.unit.hacking.test_checks \ neutron.tests.unit.hacking.test_checks \
neutron.tests.unit.common.test_config \ neutron.tests.unit.common.test_config \
neutron.tests.unit.common.test_rpc \ neutron.tests.unit.common.test_rpc \
neutron.tests.unit.common.test_log \
neutron.tests.unit.common.test_ipv6_utils \ neutron.tests.unit.common.test_ipv6_utils \
neutron.tests.unit.cmd.test_ovs_cleanup \ neutron.tests.unit.cmd.test_ovs_cleanup \
neutron.tests.unit.cmd.test_netns_cleanup \ neutron.tests.unit.cmd.test_netns_cleanup \