Sobes.tech
Junior — Senior

Decorator for logging long asynchronous functions

livecode

Task condition

It is necessary to implement a decorator slowlog that will measure the execution time of an asynchronous function. If the execution time exceeds a specified threshold (in seconds), information about such a call should be output to the console via the logging module.

import time, asyncio, logging
logging.basicConfig(level=logging.INFO)

def slowlog():
    # ...

@slowlog(limit=2)
async def test_func(wait_time):
    # ...
    time.sleep(wait_time)
    return wait_time

asyncio.run(test_func(1))  # should not log
asyncio.run(test_func(3))  # should log

The example shows how to use the decorator: when calling test_func(1) no log appears, but when calling test_func(3) a log appears because the execution time exceeds the set limit.