June 2019
Beginner to intermediate
770 pages
19h 24m
English
As a logging optimization, we might want to use a specific logger for each wrapped function and not overuse the root logger for this kind of debugging output. We'll return to the logger in Chapter 16, The Logging and Warning Modules.
The following is a version of our decorator that creates a separate logger for each individual function:
def debug2(function: F) -> F: log = logging.getLogger(function.__name__) @functools.wraps(function) def logged_function(*args, **kw): log.debug("call(%r, %r)", args, kw) result = function(*args, **kw) log.debug("result = %r", result) return result return cast(F, logged_function)
This version modifies the output to look like the following:
DEBUG:ackermann:call((2, 4), {}) DEBUG:ackermann:call((2, ...Read now
Unlock full access