Coverage for C:\src\imod-python\imod\logging\logurulogger.py: 67%

1 import sys

2

3 from loguru import logger

4

5 from imod.logging.ilogger import ILogger

6 from imod.logging.loglevel import LogLevel

7

8

9 def _depth_level(additional_depth: int) -> int:

10 """

11 The depth level is used to print the file and line number of the line being logged.

12 Because there are a few layers between the place where the imod logger is used and

13 the place where the pyton logger is used we need to set a custom stack level.

14

15 An additional_depth can be provided to add to this default depth level.

16 This is useful when a decorator is added which introduces an additional level between

17 the imod logger and the loguru logger.

18 """

19

20 default_stack_level = 2

21 return default_stack_level + additional_depth

22

23

24 class LoguruLogger(ILogger):

25 """

26 The :class:`LoguruLogger` is used to log messages using the loguru logging framework.

27 """

28

29 def __init__(

30 self,

31 log_level: LogLevel,

32 add_default_stream_handler: bool,

33 add_default_file_handler: bool,

34 ) -> None:

35 # Remove default handler set by loguru

36 logger.remove()

37

38 if add_default_stream_handler:

39 logger.add(sys.stdout, level=log_level.value)

40 if add_default_file_handler:

41 logger.add("imod-python.log", level=log_level.value)

42

43 def debug(self, message: str, additional_depth: int = 0) -> None:

44 logger.opt(depth=_depth_level(additional_depth)).debug(message)

45

46 def info(self, message: str, additional_depth: int = 0) -> None:

47 logger.opt(depth=_depth_level(additional_depth)).info(message)

48

49 def warning(self, message: str, additional_depth: int = 0) -> None:

50 logger.opt(depth=_depth_level(additional_depth)).warning(message)

51

52 def error(self, message: str, additional_depth: int = 0) -> None:

53 logger.opt(depth=_depth_level(additional_depth)).error(message)

54

55 def critical(self, message: str, additional_depth: int = 0) -> None:

56 logger.opt(depth=_depth_level(additional_depth)).critical(message)