Build a standard library logger when an *instance* is called. Sets a custom logger using :func:`logging.setLoggerClass` so variables in log format are expanded properly. >>> from structlog import configure >>> from structlog.stdlib import LoggerFactory >>> configure(logger
| 717 | |
| 718 | |
| 719 | class LoggerFactory: |
| 720 | """ |
| 721 | Build a standard library logger when an *instance* is called. |
| 722 | |
| 723 | Sets a custom logger using :func:`logging.setLoggerClass` so variables in |
| 724 | log format are expanded properly. |
| 725 | |
| 726 | >>> from structlog import configure |
| 727 | >>> from structlog.stdlib import LoggerFactory |
| 728 | >>> configure(logger_factory=LoggerFactory()) |
| 729 | |
| 730 | Args: |
| 731 | ignore_frame_names: |
| 732 | When guessing the name of a logger, skip frames whose names *start* |
| 733 | with one of these. For example, in pyramid applications you'll |
| 734 | want to set it to ``["venusian", "pyramid.config"]``. This argument |
| 735 | is called *additional_ignores* in other APIs throughout |
| 736 | *structlog*. |
| 737 | """ |
| 738 | |
| 739 | def __init__(self, ignore_frame_names: list[str] | None = None): |
| 740 | self._ignore = ignore_frame_names |
| 741 | logging.setLoggerClass(_FixedFindCallerLogger) |
| 742 | |
| 743 | def __call__(self, *args: Any) -> logging.Logger: |
| 744 | """ |
| 745 | Deduce the caller's module name and create a stdlib logger. |
| 746 | |
| 747 | If an optional argument is passed, it will be used as the logger name |
| 748 | instead of guesswork. This optional argument would be passed from the |
| 749 | :func:`structlog.get_logger` call. For example |
| 750 | ``structlog.get_logger("foo")`` would cause this method to be called |
| 751 | with ``"foo"`` as its first positional argument. |
| 752 | |
| 753 | .. versionchanged:: 0.4.0 |
| 754 | Added support for optional positional arguments. Using the first |
| 755 | one for naming the constructed logger. |
| 756 | """ |
| 757 | if args: |
| 758 | return logging.getLogger(args[0]) |
| 759 | |
| 760 | # We skip all frames that originate from within structlog or one of the |
| 761 | # configured names. |
| 762 | _, name = _find_first_app_frame_and_name(self._ignore) |
| 763 | |
| 764 | return logging.getLogger(name) |
| 765 | |
| 766 | |
| 767 | class PositionalArgumentsFormatter: |
no outgoing calls
searching dependent graphs…