-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy path03_logging.py
More file actions
50 lines (37 loc) · 1.17 KB
/
03_logging.py
File metadata and controls
50 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pprint
import inspect
import logging
import functools
logging.basicConfig(level=logging.DEBUG)
def debug(function):
@functools.wraps(function)
def _debug(*args, **kwargs):
try:
result = function(*args, **kwargs)
finally:
# Extract the signature from the function
signature = inspect.signature(function)
# Fill the arguments
arguments = signature.bind(*args, **kwargs)
# NOTE: This only works for Python 3.5 and up!
arguments.apply_defaults()
logging.debug('%s(%s): %s' % (
function.__qualname__,
', '.join('%s=%r' % (k, v) for k, v in
arguments.arguments.items()),
pprint.pformat(result),
))
return _debug
@debug
def spam(a, b=123):
return 'some spam'
spam(1)
spam(1, 456)
spam(b=1, a=456)
##############################################################################
import logging
log_format = (
'[%(relativeCreated)d %(levelname)s] '
'%(pathname)s:%(lineno)d:%(funcName)s: %(message)s'
)
logging.basicConfig(level=logging.DEBUG, format=log_format)