25
25
26
26
import logging
27
27
import typing # noqa # pylint: disable=unused-import
28
- import warnings
29
28
30
29
import six
31
30
# noinspection PyUnresolvedReferences
36
35
__all__ = ('logwrap' , 'LogWrap' )
37
36
38
37
39
- def _apply_old_spec (* args , ** kwargs ): # type: (...) -> typing.Dict[str, typing.Any]
40
- # pylint: disable=unused-argument
41
- def old_spec (
42
- log = _log_wrap_shared .logger , # type: typing.Union[logging.Logger, typing.Callable]
38
+ class LogWrap (_log_wrap_shared .BaseLogWrap ):
39
+ """LogWrap."""
40
+
41
+ __slots__ = ()
42
+
43
+ def __init__ (
44
+ self ,
45
+ func = None , # type: typing.Optional[typing.Callable]
46
+ log = _log_wrap_shared .logger , # type: logging.Logger
43
47
log_level = logging .DEBUG , # type: int
44
48
exc_level = logging .ERROR , # type: int
45
49
max_indent = 20 , # type: int
46
50
spec = None , # type: typing.Optional[typing.Callable]
47
51
blacklisted_names = None , # type: typing.Optional[typing.List[str]]
48
- blacklisted_exceptions = None , # type: typing.Optional[typing.List[Exception]]
52
+ blacklisted_exceptions = None , # type: typing.Optional[typing.List[typing.Type[ Exception] ]]
49
53
log_call_args = True , # type: bool
50
54
log_call_args_on_exc = True , # type: bool
51
55
log_result_obj = True , # type: bool
52
- ): # type: (...) -> None
53
- """Old spec."""
54
- pass # pragma: no cover
55
-
56
- # pylint: enable=unused-argument
57
-
58
- sig = funcsigs .signature (old_spec ) # type: funcsigs.Signature
59
-
60
- final_kwargs = {
61
- parameter .name : parameter .value
62
- for parameter in _log_wrap_shared .bind_args_kwargs (sig , * args , ** kwargs )
63
- } # type: typing.Dict[str, typing.Any]
64
-
65
- return final_kwargs
66
-
67
-
68
- class LogWrap (_log_wrap_shared .BaseLogWrap ):
69
- """LogWrap."""
70
-
71
- __slots__ = ()
72
-
73
- def __init__ ( # pylint: disable=keyword-arg-before-vararg
74
- self ,
75
- func = None , # type: typing.Optional[typing.Callable]
76
- * args ,
77
- ** kwargs
78
56
): # type: (...) -> None
79
57
"""Log function calls and return values.
80
58
@@ -99,7 +77,7 @@ def __init__( # pylint: disable=keyword-arg-before-vararg
99
77
:param blacklisted_names: Blacklisted argument names. Arguments with this names will be skipped in log.
100
78
:type blacklisted_names: typing.Optional[typing.Iterable[str]]
101
79
:param blacklisted_exceptions: list of exception, which should be re-raised without producing log record.
102
- :type blacklisted_exceptions: typing.Optional[typing.Iterable[Exception]]
80
+ :type blacklisted_exceptions: typing.Optional[typing.Iterable[typing.Type[ Exception] ]]
103
81
:param log_call_args: log call arguments before executing wrapped function.
104
82
:type log_call_args: bool
105
83
:param log_call_args_on_exc: log call arguments if exception raised.
@@ -109,18 +87,19 @@ def __init__( # pylint: disable=keyword-arg-before-vararg
109
87
110
88
.. versionchanged:: 3.3.0 Extract func from log and do not use Union.
111
89
"""
112
- if isinstance (func , logging .Logger ):
113
- args = (func ,) + args
114
- func = None
115
-
116
- if args :
117
- warnings .warn (
118
- 'Logwrap should use keyword-only parameters starting from version 3.4.0\n '
119
- 'After version 3.4.0 arguments list and order may be changed.' ,
120
- DeprecationWarning
121
- )
122
-
123
- super (LogWrap , self ).__init__ (func = func , ** _apply_old_spec (* args , ** kwargs ))
90
+ super (LogWrap , self ).__init__ (
91
+ func = func ,
92
+ log = log ,
93
+ log_level = log_level ,
94
+ exc_level = exc_level ,
95
+ max_indent = max_indent ,
96
+ spec = spec ,
97
+ blacklisted_names = blacklisted_names ,
98
+ blacklisted_exceptions = blacklisted_exceptions ,
99
+ log_call_args = log_call_args ,
100
+ log_call_args_on_exc = log_call_args_on_exc ,
101
+ log_result_obj = log_result_obj
102
+ )
124
103
125
104
def _get_function_wrapper (
126
105
self ,
@@ -161,10 +140,18 @@ def wrapper(*args, **kwargs):
161
140
162
141
163
142
# pylint: disable=unexpected-keyword-arg, no-value-for-parameter
164
- def logwrap ( # pylint: disable=keyword-arg-before-vararg
143
+ def logwrap (
165
144
func = None , # type: typing.Optional[typing.Callable]
166
- * args ,
167
- ** kwargs
145
+ log = _log_wrap_shared .logger , # type: logging.Logger
146
+ log_level = logging .DEBUG , # type: int
147
+ exc_level = logging .ERROR , # type: int
148
+ max_indent = 20 , # type: int
149
+ spec = None , # type: typing.Optional[typing.Callable]
150
+ blacklisted_names = None , # type: typing.Optional[typing.List[str]]
151
+ blacklisted_exceptions = None , # type: typing.Optional[typing.List[typing.Type[Exception]]]
152
+ log_call_args = True , # type: bool
153
+ log_call_args_on_exc = True , # type: bool
154
+ log_result_obj = True , # type: bool
168
155
): # type: (...) -> typing.Union[LogWrap, typing.Callable]
169
156
"""Log function calls and return values.
170
157
@@ -188,7 +175,7 @@ def logwrap( # pylint: disable=keyword-arg-before-vararg
188
175
:param blacklisted_names: Blacklisted argument names. Arguments with this names will be skipped in log.
189
176
:type blacklisted_names: typing.Optional[typing.List[str]]
190
177
:param blacklisted_exceptions: list of exception, which should be re-raised without producing log record.
191
- :type blacklisted_exceptions: typing.Optional[typing.List[Exception]]
178
+ :type blacklisted_exceptions: typing.Optional[typing.List[typing.Type[ Exception] ]]
192
179
:param log_call_args: log call arguments before executing wrapped function.
193
180
:type log_call_args: bool
194
181
:param log_call_args_on_exc: log call arguments if exception raised.
@@ -200,19 +187,17 @@ def logwrap( # pylint: disable=keyword-arg-before-vararg
200
187
201
188
.. versionchanged:: 3.3.0 Extract func from log and do not use Union.
202
189
"""
203
- if isinstance (func , logging .Logger ):
204
- args = (func , ) + args
205
- func = None
206
-
207
- if args :
208
- warnings .warn (
209
- 'Logwrap should use keyword-only parameters starting from version 3.4.0\n '
210
- 'After version 3.4.0 arguments list and order may be changed.' ,
211
- DeprecationWarning
212
- )
213
-
214
190
wrapper = LogWrap (
215
- ** _apply_old_spec (* args , ** kwargs )
191
+ log = log ,
192
+ log_level = log_level ,
193
+ exc_level = exc_level ,
194
+ max_indent = max_indent ,
195
+ spec = spec ,
196
+ blacklisted_names = blacklisted_names ,
197
+ blacklisted_exceptions = blacklisted_exceptions ,
198
+ log_call_args = log_call_args ,
199
+ log_call_args_on_exc = log_call_args_on_exc ,
200
+ log_result_obj = log_result_obj
216
201
)
217
202
if func is not None :
218
203
return wrapper (func )
0 commit comments