Coverage for C:\src\imod-python\imod\logging\logging_decorators.py: 100%

1 from time import time

2

3 from imod.logging.loglevel import LogLevel

4

5

6 def standard_log_decorator(

7 start_level: LogLevel = LogLevel.INFO, end_level: LogLevel = LogLevel.DEBUG

8):

9 """

10 Decorator to print log messages announcing the beginning and end of the decorated method

11 """

12

13 def decorator(fun):

14 def wrapper(*args, **kwargs):

15 from imod.logging import logger

16

17 # anounce start of function

18 object_name = str(type(args[0]).__name__)

19 start_message = f"Beginning execution of {fun.__module__}.{fun.__name__} for object {object_name}..."

20

21 # anounce start of function

22 start_time = time()

23 logger.log(loglevel=start_level, message=start_message, additional_depth=2)

24

25 # run function

26 return_value = fun(*args, **kwargs)

27 end_time = time()

28

29 # anounce end of function

30 end_message = f"Finished execution of {fun.__module__}.{fun.__name__} for object {object_name} in {end_time - start_time} seconds..."

31 logger.log(loglevel=end_level, message=end_message, additional_depth=2)

32 return return_value

33

34 return wrapper

35

36 return decorator

37

38

39 def init_log_decorator(

40 start_level: LogLevel = LogLevel.INFO, end_level: LogLevel = LogLevel.DEBUG

41):

42 """

43 Decorator to print log messages announcing the beginning and end of initialization methods

44 """

45

46 def decorator(fun):

47 def wrapper(*args, **kwargs):

48 from imod.logging import logger

49

50 # anounce start of function

51 object_name = str(type(args[0]).__name__)

52 start_message = f"Initializing the {object_name} package..."

53 start_time = time()

54 logger.log(loglevel=start_level, message=start_message, additional_depth=2)

55

56 # run function

57 return_value = fun(*args, **kwargs)

58 end_time = time()

59

60 # anounce end of function

61 end_message = f"Successfully initialized the {object_name} in {end_time - start_time} seconds..."

62 logger.log(loglevel=end_level, message=end_message, additional_depth=2)

63 return return_value

64

65 return wrapper

66

67 return decorator