Skip to content

Commit

Permalink
fix: Reuse existing loggers, preventing overwriting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Jul 14, 2024
1 parent 06b383b commit 3c2825f
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/_griffe/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,25 @@ class _Logger:
def __init__(self, name: str) -> None:
# Default logger that can be patched by third-party.
self._logger = self.__class__._default_logger(name)
# Register instance.
self._instances[name] = self

def __getattr__(self, name: str) -> Any:
# Forward everything to the logger.
return getattr(self._logger, name)

@classmethod
def get(cls, name: str) -> _Logger:
"""Get a logger instance.
Parameters:
name: The logger name.
Returns:
The logger instance.
"""
if name not in cls._instances:
cls._instances[name] = cls(name)
return cls._instances[name]

@classmethod
def _patch_loggers(cls, get_logger_func: Callable) -> None:
# Patch current instances.
Expand All @@ -67,7 +79,7 @@ def get_logger(name: str) -> _Logger:
Returns:
The logger.
"""
return _Logger(name)
return _Logger.get(name)


def patch_loggers(get_logger_func: Callable[[str], Any]) -> None:
Expand Down

0 comments on commit 3c2825f

Please sign in to comment.