Coverage for src/abcd_graph/logger.py: 100%
37 statements
« prev ^ index » next coverage.py v7.5.3, created at 2024-12-04 21:31 +0100
« prev ^ index » next coverage.py v7.5.3, created at 2024-12-04 21:31 +0100
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.
21__all__ = [
22 "construct_logger",
23]
25import logging
26import os
27from abc import (
28 ABC,
29 abstractmethod,
30)
31from typing import Union
33from typing_extensions import TypeAlias
36class ABCDLogger(ABC): # pragma: no cover
37 @abstractmethod
38 def info(self, message: str) -> None:
39 pass
41 @abstractmethod
42 def debug(self, message: str) -> None:
43 pass
45 @abstractmethod
46 def warning(self, message: str) -> None:
47 pass
49 @abstractmethod
50 def error(self, message: str) -> None:
51 pass
53 @abstractmethod
54 def critical(self, message: str) -> None:
55 pass
58class NoOpLogger(ABCDLogger):
59 def info(self, message: str) -> None:
60 pass
62 def debug(self, message: str) -> None:
63 pass
65 def warning(self, message: str) -> None:
66 pass
68 def error(self, message: str) -> None:
69 pass
71 def critical(self, message: str) -> None:
72 pass
75class StdOutLogger(ABCDLogger):
76 NAME = "abcd-graph"
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)
87 def info(self, message: str) -> None:
88 self.logger.info(message)
90 def debug(self, message: str) -> None:
91 self.logger.debug(message)
93 def warning(self, message: str) -> None:
94 self.logger.warning(message)
96 def error(self, message: str) -> None:
97 self.logger.error(message)
99 def critical(self, message: str) -> None:
100 self.logger.critical(message)
103LoggerType: TypeAlias = Union[ABCDLogger, bool]
106def construct_logger(logger: bool) -> ABCDLogger:
107 return StdOutLogger() if logger else NoOpLogger()