summaryrefslogtreecommitdiff
path: root/openstackclient/common/utils.py
diff options
context:
space:
mode:
authorAkihiro Motoki <motoki@da.jp.nec.com>2016-01-09 13:43:36 +0900
committerAkihiro Motoki <motoki@da.jp.nec.com>2016-01-20 22:02:19 +0900
commit50d54bb007c2531013e9b7ae358737dcc19c519b (patch)
treed76602a9cbc6aaa3131c554858352c9cfa0c5e63 /openstackclient/common/utils.py
parent0e6b86ad94819fcb7bf20e3368b86a504d6c6702 (diff)
downloadpython-openstackclient-50d54bb007c2531013e9b7ae358737dcc19c519b.tar.gz
log_method: get logger from decorated method if unspecified
This commit makes 'log' optional. 'log' attribute of each command class does not exist when the class is defined because 'log' is now setup dynamically when a class is instantiated. Instead log_method looks for a logger from a decorating method. compute.v2.server is changed in this commit as an example. Change-Id: Ic4d128f8e027d3b8e6f884f31369e9085c0f0871 Partial-Bug: #1532294
Diffstat (limited to 'openstackclient/common/utils.py')
-rw-r--r--openstackclient/common/utils.py22
1 files changed, 13 insertions, 9 deletions
diff --git a/openstackclient/common/utils.py b/openstackclient/common/utils.py
index 096c995b..3ae30c8f 100644
--- a/openstackclient/common/utils.py
+++ b/openstackclient/common/utils.py
@@ -26,28 +26,32 @@ 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."""
+class log_method(object):
- def decorator(func):
+ def __init__(self, log=None, level=logging.DEBUG):
+ self._log = log
+ self._level = level
+
+ def __call__(self, func):
func_name = func.__name__
+ if not self._log:
+ self._log = logging.getLogger(func.__class__.__name__)
@six.wraps(func)
- def wrapper(self, *args, **kwargs):
- if log.isEnabledFor(level):
+ def wrapper(*args, **kwargs):
+ if self._log.isEnabledFor(self._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)
+ self._log.log(self._level, "%s(%s)",
+ func_name, ", ".join(pretty_args))
+ return func(*args, **kwargs)
return wrapper
- return decorator
-
def find_resource(manager, name_or_id, **kwargs):
"""Helper for the _find_* methods.