MCPcopy Create free account
hub / github.com/hynek/structlog / as_immutable

Function as_immutable

src/structlog/threadlocal.py:105–135  ·  view source on GitHub ↗

Extract the context from a thread local logger into an immutable logger. Args: logger (structlog.typing.BindableLogger): A logger with *possibly* thread local state. Returns: :class:`~structlog.BoundLogger` with an immutable context. .. deprecated:: 22

(logger: TLLogger)

Source from the content-addressed store, hash-verified

103
104
105def as_immutable(logger: TLLogger) -> TLLogger:
106 """
107 Extract the context from a thread local logger into an immutable logger.
108
109 Args:
110 logger (structlog.typing.BindableLogger):
111 A logger with *possibly* thread local state.
112
113 Returns:
114 :class:`~structlog.BoundLogger` with an immutable context.
115
116 .. deprecated:: 22.1.0
117 """
118 _deprecated()
119 if isinstance(logger, BoundLoggerLazyProxy):
120 logger = logger.bind()
121
122 try:
123 ctx = logger._context._tl.dict_.__class__( # type: ignore[attr-defined]
124 logger._context._dict # type: ignore[attr-defined]
125 )
126 bl = logger.__class__(
127 logger._logger, # type: ignore[attr-defined, call-arg]
128 processors=logger._processors, # type: ignore[attr-defined]
129 context={},
130 )
131 bl._context = ctx # type: ignore[misc]
132
133 return bl
134 except AttributeError:
135 return logger
136
137
138@contextlib.contextmanager

Callers 4

test_converts_proxyMethod · 0.90
test_idempotencyMethod · 0.90
tmp_bindFunction · 0.85

Calls 2

_deprecatedFunction · 0.85
bindMethod · 0.45

Tested by 3

test_converts_proxyMethod · 0.72
test_idempotencyMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…