Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Incremental improvement to application logging #5505

Merged
merged 4 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/poetry/console/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions src/poetry/console/logging/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from __future__ import annotations

import logging


POETRY_FILTER = logging.Filter(name="poetry")
7 changes: 6 additions & 1 deletion src/poetry/console/logging/io_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from typing import TYPE_CHECKING

from poetry.console.logging.filters import POETRY_FILTER
from poetry.console.logging.formatters import FORMATTERS


Expand All @@ -30,6 +31,10 @@ def format(self, record: LogRecord) -> str:
elif level in self._colors:
msg = f"<{self._colors[level]}>{msg}</>"

return msg
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)
5 changes: 2 additions & 3 deletions src/poetry/repositories/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<debug>{self.name}:</debug> {msg}"
)
logger = logging.getLogger(f"{__name__}.{self.__class__.__name__}")
getattr(logger, level)(f"<debug>{self.name}:</debug> {msg}")

def __len__(self) -> int:
return len(self._packages)
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import os
import re
import shutil
Expand Down Expand Up @@ -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"))