loggext.decorators
add_logging¶
The add_logging
decorator can quickly add logging to a function or method which can help during debugging.
The logging-messages include:
- when the function is called and with which arguments
- when the function returns, how long it took and what was returned
- when the function failed and an exception is raised
import logging
from loggext.decorators import add_logging
logger = logging.getLogger(__name__)
@add_logging(
logger=logger, # (1)
call=True, # (2)
call_args=True, # (3)
timeit=True, # (4)
timeit_precision=2, # (5)
result=True, # (6)
)
def myfn(arg):
... # your code
myfn("value")
# DEBUG:root:<function myfn at 0x7f06a6cc3400> was called with ('value')
# DEBUG:root:<function myfn at 0x7f06a6cc3400> returned None after 65μs+614ns
- species a custom logger
- whether to log when the function is called
- whether to log the passed arguments of the function-call.
- whether to measure the performance of the function
- number of units when formatting the timing.
1: 65μs
2: 65μs+614ns - whether to log the returned value
async functions¶
The add_logging
decorator also supports asnyc-functions.
from loggext.decorators import add_logging
@add_logging()
async def async_function():
... # your code
Performance¶
Worried about the performance? You don't need to!
If your logger is not configured for logging.DEBUG
messages then they are neither generated nor logged.
This way no computation is wasted to e.g. convert the complex function-arguments to their representative form.