log.py 955 B

12345678910111213141516171819202122232425262728293031
  1. """Grab the global logger instance."""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from __future__ import annotations
  5. import logging
  6. from typing import Any
  7. _logger: logging.Logger | logging.LoggerAdapter[Any] | None = None
  8. def get_logger() -> logging.Logger | logging.LoggerAdapter[Any]:
  9. """Grab the global logger instance.
  10. If a global Application is instantiated, grab its logger.
  11. Otherwise, grab the root logger.
  12. """
  13. global _logger # noqa: PLW0603
  14. if _logger is None:
  15. from .config import Application
  16. if Application.initialized():
  17. _logger = Application.instance().log
  18. else:
  19. _logger = logging.getLogger("traitlets")
  20. # Add a NullHandler to silence warnings about not being
  21. # initialized, per best practice for libraries.
  22. _logger.addHandler(logging.NullHandler())
  23. return _logger