_generic.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the MIT License. See the LICENSE file in the root of this
  3. # repository for complete details.
  4. """
  5. Generic bound logger that can wrap anything.
  6. """
  7. from __future__ import absolute_import, division, print_function
  8. from functools import partial
  9. from structlog._base import BoundLoggerBase
  10. class BoundLogger(BoundLoggerBase):
  11. """
  12. A generic BoundLogger that can wrap anything.
  13. Every unknown method will be passed to the wrapped logger. If that's too
  14. much magic for you, try :class:`structlog.stdlib.BoundLogger` or
  15. :class:`structlog.twisted.BoundLogger` which also take advantage of
  16. knowing the wrapped class which generally results in better performance.
  17. Not intended to be instantiated by yourself. See
  18. :func:`~structlog.wrap_logger` and :func:`~structlog.get_logger`.
  19. """
  20. def __getattr__(self, method_name):
  21. """
  22. If not done so yet, wrap the desired logger method & cache the result.
  23. """
  24. wrapped = partial(self._proxy_to_logger, method_name)
  25. setattr(self, method_name, wrapped)
  26. return wrapped