File size: 789 Bytes
868b252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from functools import wraps
from time import time
from typing import Any, Callable, Literal

from loguru import logger

Log_Level = Literal[
    "TRACE",
    "DEBUG",
    "INFO",
    "SUCCESS",
    "WARNING",
    "ERROR",
    "CRITICAL",
]


def timed_function(level: Log_Level = "INFO") -> Callable[..., Any]:
    def decorator(func: Any) -> Callable[..., Any]:
        @wraps(func)
        def wrapper(*args: Any, **kwargs: Any) -> Any:
            start_time = time()
            result = func(*args, **kwargs)
            execution_time = time() - start_time
            logger.log(
                level,
                f"Function '{func.__qualname__}' executed in {execution_time:.4f} seconds",
            )

            return result

        return wrapper

    return decorator