File size: 843 Bytes
cf71c95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
import time
import functools
import logging
import logging.config
import sys
from typing import Callable, Any, TypeVar, cast

# TypeVar for better type hints
F = TypeVar('F', bound=Callable[..., Any])


def timeit(func: F) -> F:
    """
    Decorator that logs the execution time of the decorated function.

    Args:
        func: The function to be timed

    Returns:
        The wrapped function with timing functionality
    """

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        execution_time = end_time - start_time

        logger = logging.getLogger(func.__module__)
        logger.info(f"Function '{func.__name__}' executed in {execution_time:.4f} seconds")

        return result

    return cast(F, wrapper)