Skip to content

Commit 50d54bb

Browse files
author
Akihiro Motoki
committed
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
1 parent 0e6b86a commit 50d54bb

2 files changed

Lines changed: 44 additions & 99 deletions

File tree

openstackclient/common/utils.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,32 @@
2626
from openstackclient.common import exceptions
2727

2828

29-
def log_method(log, level=logging.DEBUG):
30-
"""Logs a method and its arguments when entered."""
29+
class log_method(object):
3130

32-
def decorator(func):
31+
def __init__(self, log=None, level=logging.DEBUG):
32+
self._log = log
33+
self._level = level
34+
35+
def __call__(self, func):
3336
func_name = func.__name__
37+
if not self._log:
38+
self._log = logging.getLogger(func.__class__.__name__)
3439

3540
@six.wraps(func)
36-
def wrapper(self, *args, **kwargs):
37-
if log.isEnabledFor(level):
41+
def wrapper(*args, **kwargs):
42+
if self._log.isEnabledFor(self._level):
3843
pretty_args = []
3944
if args:
4045
pretty_args.extend(str(a) for a in args)
4146
if kwargs:
4247
pretty_args.extend(
4348
"%s=%s" % (k, v) for k, v in six.iteritems(kwargs))
44-
log.log(level, "%s(%s)", func_name, ", ".join(pretty_args))
45-
return func(self, *args, **kwargs)
49+
self._log.log(self._level, "%s(%s)",
50+
func_name, ", ".join(pretty_args))
51+
return func(*args, **kwargs)
4652

4753
return wrapper
4854

49-
return decorator
50-
5155

5256
def find_resource(manager, name_or_id, **kwargs):
5357
"""Helper for the _find_* methods.

0 commit comments

Comments
 (0)