Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaminsk committed Feb 23, 2024
1 parent 57e71cd commit fc4dd6c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
14 changes: 12 additions & 2 deletions src/fillname/utils/logging.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
"""
Setup project wide loggers.
This is a thin wrapper around Python's logging module. It supports colored
logging.
"""

import logging
from typing import TextIO

NOTSET = logging.NOTSET
DEBUG = logging.DEBUG
INFO = logging.INFO
WARNING = logging.WARNING
ERROR = logging.ERROR
CRITICAL = logging.CRITICAL

COLORS = {
"GREY": "\033[90m",
"BLUE": "\033[94m",
Expand Down Expand Up @@ -41,7 +51,7 @@ def configure_logging(stream: TextIO, level: int) -> None:
"""
log_message_str = "{}%(levelname)s:{} - %(message)s{}"

def make_handler(level: int, color: str) -> logging.StreamHandler:
def make_handler(level: int, color: str) -> "logging.StreamHandler[TextIO]":
handler = logging.StreamHandler(stream)
handler.addFilter(SingleLevelFilter(level, False))
handler.setLevel(level)
Expand All @@ -58,7 +68,7 @@ def make_handler(level: int, color: str) -> logging.StreamHandler:
logging.basicConfig(handlers=handlers, level=level)


def get_logger(name):
def get_logger(name: str) -> logging.Logger:
"""
Get a logger with the given name.
"""
Expand Down
4 changes: 2 additions & 2 deletions src/fillname/utils/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
The command line parser for the project.
"""

import logging
import sys
from argparse import ArgumentParser
from textwrap import dedent
from typing import Any, Optional, cast

from . import logging

__all__ = ["get_parser"]

if sys.version_info[1] < 8:
Expand All @@ -31,7 +32,6 @@ def get_parser() -> ArgumentParser:
"""
),
)

levels = [
("error", logging.ERROR),
("warning", logging.WARNING),
Expand Down
10 changes: 4 additions & 6 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Test cases for main application functionality.
"""

import logging
from io import StringIO
from unittest import TestCase

from fillname.utils.logger import setup_logger
from fillname.utils import logging
from fillname.utils.logging import configure_logging, get_logger
from fillname.utils.parser import get_parser


Expand All @@ -19,11 +19,9 @@ def test_logger(self) -> None:
"""
Test the logger.
"""
log = setup_logger("global", logging.INFO)
sio = StringIO()
for handler in log.handlers:
assert isinstance(handler, logging.StreamHandler)
handler.setStream(sio)
configure_logging(sio, logging.INFO)
log = get_logger("main")
log.info("test123")
self.assertRegex(sio.getvalue(), "test123")

Expand Down

0 comments on commit fc4dd6c

Please sign in to comment.