diff options
| author | Joshua Harlow <harlowja@yahoo-inc.com> | 2015-09-01 16:43:07 -0700 |
|---|---|---|
| committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2015-09-01 16:49:54 -0700 |
| commit | e3c46ece4a496584a54b9d39b55921990db4a7b3 (patch) | |
| tree | aadf0d2f80f18d77ebc003be3168d36dcb01a275 /openstackclient/common/utils.py | |
| parent | f14251669f96d6010581702417828f4380144aa2 (diff) | |
| download | python-openstackclient-e3c46ece4a496584a54b9d39b55921990db4a7b3.tar.gz | |
Use a common decorator to log 'take_action' activation
Instead of duplicating the same log statement throughout
the code, the same logic can be provided by a shared decorator
that abstracts away the logging capability and unifies it behind
a common function instead.
Change-Id: Icc63bced7347c8bbf0299a4c5821425a10892a79
Diffstat (limited to 'openstackclient/common/utils.py')
| -rw-r--r-- | openstackclient/common/utils.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/openstackclient/common/utils.py b/openstackclient/common/utils.py index 2f8419f4..b6726bfa 100644 --- a/openstackclient/common/utils.py +++ b/openstackclient/common/utils.py @@ -26,6 +26,29 @@ from oslo_utils import importutils from openstackclient.common import exceptions +def log_method(log, level=logging.DEBUG): + """Logs a method and its arguments when entered.""" + + def decorator(func): + func_name = func.__name__ + + @six.wraps(func) + def wrapper(self, *args, **kwargs): + if log.isEnabledFor(level): + pretty_args = [] + if args: + pretty_args.extend(str(a) for a in args) + if kwargs: + pretty_args.extend( + "%s=%s" % (k, v) for k, v in six.iteritems(kwargs)) + log.log(level, "%s(%s)", func_name, ", ".join(pretty_args)) + return func(self, *args, **kwargs) + + return wrapper + + return decorator + + def find_resource(manager, name_or_id, **kwargs): """Helper for the _find_* methods. |
