-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecorator.py
29 lines (27 loc) · 1.09 KB
/
decorator.py
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
import asyncio
import logging
import time
from functools import wraps
def log_execution_time(target_logger: logging.Logger):
def decorator(function):
if asyncio.iscoroutinefunction(function):
@wraps(function)
async def async_wrapper(*args, **kwargs):
start_time = time.monotonic()
result = await function(*args, **kwargs)
end_time = time.monotonic()
elapsed_time = end_time - start_time
target_logger.debug(f"{function.__name__} executed in {elapsed_time:.4f} seconds")
return result
return async_wrapper
else:
@wraps(function)
def sync_wrapper(*args, **kwargs):
start_time = time.monotonic()
result = function(*args, **kwargs)
end_time = time.monotonic()
elapsed_time = end_time - start_time
target_logger.debug(f"{function.__name__} executed in {elapsed_time:.4f} seconds")
return result
return sync_wrapper
return decorator