From 1117060e7fedc9c247c34d24309db4f40b776674 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Tue, 26 Apr 2022 20:29:59 +0200 Subject: [PATCH 1/4] ensure console logs handle format strings correctly --- src/poetry/console/logging/io_formatter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/poetry/console/logging/io_formatter.py b/src/poetry/console/logging/io_formatter.py index aa3630f380a..e81dace0423 100644 --- a/src/poetry/console/logging/io_formatter.py +++ b/src/poetry/console/logging/io_formatter.py @@ -30,6 +30,6 @@ def format(self, record: LogRecord) -> str: elif level in self._colors: msg = f"<{self._colors[level]}>{msg}" - return msg + record.msg = msg return super().format(record) From b43dcd9c1112c61af64cc25911655e7127f87e40 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Tue, 26 Apr 2022 20:33:47 +0200 Subject: [PATCH 2/4] log only poetry messages unless very verbose --- src/poetry/console/application.py | 5 +++++ src/poetry/console/logging/filters.py | 6 ++++++ src/poetry/repositories/repository.py | 5 ++--- 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 src/poetry/console/logging/filters.py diff --git a/src/poetry/console/application.py b/src/poetry/console/application.py index 9f3ca17cedb..9d9c7ed581a 100644 --- a/src/poetry/console/application.py +++ b/src/poetry/console/application.py @@ -212,6 +212,7 @@ def _configure_io(self, io: IO) -> None: def register_command_loggers( self, event: ConsoleCommandEvent, event_name: str, _: Any ) -> None: + from poetry.console.logging.filters import POETRY_FILTER from poetry.console.logging.io_formatter import IOFormatter from poetry.console.logging.io_handler import IOHandler @@ -241,6 +242,10 @@ def register_command_loggers( logging.basicConfig(level=level, handlers=[handler]) + # only log third-party packages when very verbose + if not io.is_very_verbose(): + handler.addFilter(POETRY_FILTER) + for name in loggers: logger = logging.getLogger(name) diff --git a/src/poetry/console/logging/filters.py b/src/poetry/console/logging/filters.py new file mode 100644 index 00000000000..3ae42b5e92e --- /dev/null +++ b/src/poetry/console/logging/filters.py @@ -0,0 +1,6 @@ +from __future__ import annotations + +import logging + + +POETRY_FILTER = logging.Filter(name="poetry") diff --git a/src/poetry/repositories/repository.py b/src/poetry/repositories/repository.py index ed624ad02f6..7a91bd23d5d 100644 --- a/src/poetry/repositories/repository.py +++ b/src/poetry/repositories/repository.py @@ -114,9 +114,8 @@ def _get_constraints_from_dependency( return constraint, allow_prereleases def _log(self, msg: str, level: str = "info") -> None: - getattr(logging.getLogger(self.__class__.__name__), level)( - f"{self.name}: {msg}" - ) + logger = logging.getLogger(f"{__name__}.{self.__class__.__name__}") + getattr(logger, level)(f"{self.name}: {msg}") def __len__(self) -> int: return len(self._packages) From 6624d8db05c069832f30792f7c05453627021e70 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Tue, 26 Apr 2022 21:57:18 +0200 Subject: [PATCH 3/4] prefix third-party package logs with name --- src/poetry/console/logging/io_formatter.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/poetry/console/logging/io_formatter.py b/src/poetry/console/logging/io_formatter.py index e81dace0423..3f2242d87ff 100644 --- a/src/poetry/console/logging/io_formatter.py +++ b/src/poetry/console/logging/io_formatter.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING +from poetry.console.logging.filters import POETRY_FILTER from poetry.console.logging.formatters import FORMATTERS @@ -32,4 +33,8 @@ def format(self, record: LogRecord) -> str: record.msg = msg + if not POETRY_FILTER.filter(record): + # prefix third-party packages with name for easier debugging + record.msg = f"[{record.name}] {record.msg}" + return super().format(record) From 54b86635fb24f4a147d6267dfefb7dca1caee836 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Tue, 26 Apr 2022 21:56:52 +0200 Subject: [PATCH 4/4] tests: use simple log formatting --- tests/conftest.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index d4369240feb..d2d52feb0c6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ from __future__ import annotations +import logging import os import re import shutil @@ -402,3 +403,14 @@ def _factory( @pytest.fixture def project_root() -> Path: return Path(__file__).parent.parent + + +@pytest.fixture(autouse=True) +def set_simple_log_formatter() -> None: + """ + This fixture removes any formatting added via IOFormatter. + """ + for name in logging.Logger.manager.loggerDict: + for handler in logging.getLogger(name).handlers: + # replace formatter with simple formatter for testing + handler.setFormatter(logging.Formatter(fmt="%(message)s"))