diff --git a/src/fillname/__main__.py b/src/fillname/__main__.py index 9523db0..10f5353 100644 --- a/src/fillname/__main__.py +++ b/src/fillname/__main__.py @@ -14,7 +14,7 @@ def main() -> None: """ parser = get_parser() args = parser.parse_args() - configure_logging(sys.stderr, args.log) + configure_logging(sys.stderr, args.log, sys.stderr.isatty()) log = get_logger("main") log.info("info") diff --git a/src/fillname/utils/logging.py b/src/fillname/utils/logging.py index a82eeb8..d18de14 100644 --- a/src/fillname/utils/logging.py +++ b/src/fillname/utils/logging.py @@ -45,17 +45,20 @@ def filter(self, record: logging.LogRecord) -> bool: return record.levelno == self.passlevel -def configure_logging(stream: TextIO, level: int) -> None: +def configure_logging(stream: TextIO, level: int, use_color: bool) -> None: """ Configure application logging. """ - log_message_str = "{}%(levelname)s:{} - %(message)s{}" + def format_str(color: str) -> str: + if use_color: + return f"{COLORS[color]}%(levelname)s:{COLORS['GREY']} - %(message)s{COLORS['NORMAL']}" + return "%(levelname)s: - %(message)s" # nocoverage def make_handler(level: int, color: str) -> "logging.StreamHandler[TextIO]": 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"])) + formatter = logging.Formatter(format_str(color)) handler.setFormatter(formatter) return handler diff --git a/tests/test_main.py b/tests/test_main.py index 38d40dd..26f1aac 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -20,7 +20,7 @@ def test_logger(self) -> None: Test the logger. """ sio = StringIO() - configure_logging(sio, logging.INFO) + configure_logging(sio, logging.INFO, True) log = get_logger("main") log.info("test123") self.assertRegex(sio.getvalue(), "test123")