diff --git a/src/fillname/__main__.py b/src/fillname/__main__.py index 5f48055..9523db0 100644 --- a/src/fillname/__main__.py +++ b/src/fillname/__main__.py @@ -2,7 +2,9 @@ The main entry point for the application. """ -from .utils.logger import setup_logger +import sys + +from .utils.logging import configure_logging, get_logger from .utils.parser import get_parser @@ -12,8 +14,9 @@ def main() -> None: """ parser = get_parser() args = parser.parse_args() - log = setup_logger("main", args.log) + configure_logging(sys.stderr, args.log) + log = get_logger("main") log.info("info") log.warning("warning") log.debug("debug") diff --git a/src/fillname/utils/logger.py b/src/fillname/utils/logging.py similarity index 61% rename from src/fillname/utils/logger.py rename to src/fillname/utils/logging.py index a729a72..853c2aa 100644 --- a/src/fillname/utils/logger.py +++ b/src/fillname/utils/logging.py @@ -3,7 +3,7 @@ """ import logging -import sys +from typing import TextIO COLORS = { "GREY": "\033[90m", @@ -35,27 +35,31 @@ def filter(self, record: logging.LogRecord) -> bool: return record.levelno == self.passlevel -def setup_logger(name: str, level: int) -> logging.Logger: +def configure_logging(stream: TextIO, level: int) -> None: """ - Setup logger. + Configure application logging. """ - - logger = logging.getLogger(name) - logger.propagate = False - logger.setLevel(level) log_message_str = "{}%(levelname)s:{} - %(message)s{}" - def set_handler(level: int, color: str) -> None: - handler = logging.StreamHandler(sys.stderr) + def make_handler(level: int, color: str) -> logging.StreamHandler: + handler = logging.StreamHandler(stream) handler.addFilter(SingleLevelFilter(level, False)) handler.setLevel(level) formatter = logging.Formatter(log_message_str.format(COLORS[color], COLORS["GREY"], COLORS["NORMAL"])) handler.setFormatter(formatter) - logger.addHandler(handler) + return handler + + handlers = [ + make_handler(logging.INFO, "GREEN"), + make_handler(logging.WARNING, "YELLOW"), + make_handler(logging.DEBUG, "BLUE"), + make_handler(logging.ERROR, "RED"), + ] + logging.basicConfig(handlers=handlers, level=level) - set_handler(logging.INFO, "GREEN") - set_handler(logging.WARNING, "YELLOW") - set_handler(logging.DEBUG, "BLUE") - set_handler(logging.ERROR, "RED") - return logger +def get_logger(name): + """ + Get a logger with the given name. + """ + return logging.getLogger(name)