diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2023-02-19 22:59:32 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2023-02-19 22:59:32 -0600 |
commit | e28fbb181851ca16bc0ade9c371628f86c25adbc (patch) | |
tree | 35db0440058600b36655956c61e080a08cb9d7ee /src | |
parent | b2fbb532e359985142a71354b5b648ae560a80ac (diff) |
refactor custom logger and add tests
Diffstat (limited to 'src')
-rw-r--r-- | src/pyssg/__init__.py | 16 | ||||
-rw-r--r-- | src/pyssg/custom_logger.py (renamed from src/pyssg/per_level_formatter.py) | 18 |
2 files changed, 20 insertions, 14 deletions
diff --git a/src/pyssg/__init__.py b/src/pyssg/__init__.py index a4e5857..5d112f1 100644 --- a/src/pyssg/__init__.py +++ b/src/pyssg/__init__.py @@ -1,17 +1,7 @@ -from logging import Logger, StreamHandler, getLogger, INFO - from .pyssg import main -from .per_level_formatter import PerLevelFormatter - +from .custom_logger import setup_logger -# since this is the root package, setup the logger here -__LOG_LEVEL: int = INFO -log: Logger = getLogger(__name__) -log.setLevel(__LOG_LEVEL) -ch: StreamHandler = StreamHandler() -ch.setLevel(__LOG_LEVEL) -ch.setFormatter(PerLevelFormatter()) -log.addHandler(ch) +setup_logger() # not meant to be used as a package, so just give main -__all__ = ['main']
\ No newline at end of file +__all__ = ['main'] diff --git a/src/pyssg/per_level_formatter.py b/src/pyssg/custom_logger.py index e3b6977..55f3e2d 100644 --- a/src/pyssg/per_level_formatter.py +++ b/src/pyssg/custom_logger.py @@ -1,4 +1,11 @@ -from logging import Formatter, LogRecord, DEBUG, INFO, WARNING, ERROR, CRITICAL +import sys +from logging import (Logger, StreamHandler, Formatter, LogRecord, + DEBUG, INFO, WARNING, ERROR, CRITICAL, + getLogger) + +LOG_LEVEL: int = INFO +# 'pyssg' es the name of the root logger +LOGGER_NAME: str = 'pyssg' # only reason for this class is to get info formatting as normal text @@ -27,3 +34,12 @@ class PerLevelFormatter(Formatter): formatter: Formatter = Formatter( fmt=fmt, datefmt=self.__DATE_FMT, style='%') return formatter.format(record) + + +def setup_logger(name: str = LOGGER_NAME, level: int = LOG_LEVEL) -> None: + logger: Logger = getLogger(name) + handler: StreamHandler = StreamHandler(sys.stdout) + logger.setLevel(level) + handler.setLevel(level) + handler.setFormatter(PerLevelFormatter()) + logger.addHandler(handler) |