Coverage for src/abcd_graph/logger.py: 100%

1# Copyright (c) 2024 Jordan Barrett & Aleksander Wojnarowicz

2#

3# Permission is hereby granted, free of charge, to any person obtaining a copy

4# of this software and associated documentation files (the "Software"), to deal

5# in the Software without restriction, including without limitation the rights

6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

7# copies of the Software, and to permit persons to whom the Software is

8# furnished to do so, subject to the following conditions:

9#

10# The above copyright notice and this permission notice shall be included in all

11# copies or substantial portions of the Software.

12#

13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

19# SOFTWARE.

20

21 __all__ = [

22 "construct_logger",

23]

24

25 import logging

26 import os

27 from abc import (

28 ABC,

29 abstractmethod,

30)

31 from typing import Union

32

33 from typing_extensions import TypeAlias

34

35

36 class ABCDLogger(ABC): # pragma: no cover

37 @abstractmethod

38 def info(self, message: str) -> None:

39 pass

40

41 @abstractmethod

42 def debug(self, message: str) -> None:

43 pass

44

45 @abstractmethod

46 def warning(self, message: str) -> None:

47 pass

48

49 @abstractmethod

50 def error(self, message: str) -> None:

51 pass

52

53 @abstractmethod

54 def critical(self, message: str) -> None:

55 pass

56

57

58 class NoOpLogger(ABCDLogger):

59 def info(self, message: str) -> None:

60 pass

61

62 def debug(self, message: str) -> None:

63 pass

64

65 def warning(self, message: str) -> None:

66 pass

67

68 def error(self, message: str) -> None:

69 pass

70

71 def critical(self, message: str) -> None:

72 pass

73

74

75 class StdOutLogger(ABCDLogger):

76 NAME = "abcd-graph"

77

78 def __init__(self) -> None:

79 logging_level = int(os.getenv("ABCD_LOG", logging.INFO))

80 logging.basicConfig(

81 level=logging_level,

82 format="[%(name)s] - %(asctime)s - %(levelname)s - %(message)s",

83 )

84 self.logging_level = logging_level

85 self.logger = logging.getLogger(self.NAME)

86

87 def info(self, message: str) -> None:

88 self.logger.info(message)

89

90 def debug(self, message: str) -> None:

91 self.logger.debug(message)

92

93 def warning(self, message: str) -> None:

94 self.logger.warning(message)

95

96 def error(self, message: str) -> None:

97 self.logger.error(message)

98

99 def critical(self, message: str) -> None:

100 self.logger.critical(message)

101

102

103 LoggerType: TypeAlias = Union[ABCDLogger, bool]

104

105

106 def construct_logger(logger: bool) -> ABCDLogger:

107 return StdOutLogger() if logger else NoOpLogger()