On this page
logging.LoggerAdapter
Prev: logging.Logger | Up: logging | Next: logging.LogRecord
Rel: logging.addLevelName, logging.basicConfig, logging.BufferingFormatter, logging.captureWarnings
Ref: docs.python.org — logging.LoggerAdapter
Tags: #public #python3 #stdlib #logging
Import
import logging
from logging import LoggerAdapter
Signature & Overview
logging.LoggerAdapter(logger, extra=None, merge_extra=False)
An adapter for loggers which makes it easier to specify contextual information in logging output.
Methods & Attributes
LoggerAdapter.critical(self, msg, *args, **kwargs)— Delegate a critical call to the underlying logger.LoggerAdapter.debug(self, msg, *args, **kwargs)— Delegate a debug call to the underlying logger.LoggerAdapter.error(self, msg, *args, **kwargs)— Delegate an error call to the underlying logger.LoggerAdapter.exception(self, msg, *args, exc_info=True, **kwargs)— Delegate an exception call to the underlying logger.LoggerAdapter.getEffectiveLevel(self)— Get the effective level for the underlying logger.LoggerAdapter.hasHandlers(self)— See if the underlying logger has any handlers.LoggerAdapter.info(self, msg, *args, **kwargs)— Delegate an info call to the underlying logger.LoggerAdapter.isEnabledFor(self, level)— Is this logger enabled for level 'level'?LoggerAdapter.log(self, level, msg, *args, **kwargs)— Delegate a log call to the underlying logger, after adding contextual information from this adapter instance.LoggerAdapter.manager(...)—LoggerAdapter.managermethod/property.LoggerAdapter.name(...)—LoggerAdapter.namemethod/property.LoggerAdapter.process(self, msg, kwargs)— Process the logging message and keyword arguments passed in to a logging call to insert contextual information. You can either manipulate the message itself, the keyword args or both. ReturnLoggerAdapter.setLevel(self, level)— Set the specified level on the underlying logger.LoggerAdapter.warn(self, msg, *args, **kwargs)—LoggerAdapter.warnmethod/property.LoggerAdapter.warning(self, msg, *args, **kwargs)— Delegate a warning call to the underlying logger.
Usage
import io
import logging
buf = io.StringIO()
base = logging.getLogger("demo.adapter")
base.setLevel(logging.INFO)
h = logging.StreamHandler(buf)
h.setFormatter(logging.Formatter("[%(req_id)s] %(message)s"))
base.addHandler(h)
try:
adapter = logging.LoggerAdapter(base, {"req_id": "abc-123"})
adapter.info("handled")
finally:
base.removeHandler(h)
assert buf.getvalue().strip() == "[abc-123] handled"
Gotchas
- Injects contextual
extrainto every log call: Cleaner than repeatingextra={...}on everylogger.info()line. - Merging per-call
extra=: Passmerge_extra=True(Python 3.13+) so call-siteextramerges with adapter context instead of being overwritten.
REPL Inspect
from logging import LoggerAdapter
help(LoggerAdapter)
Linked from
Related notes
- tuts/python3/stdlib_module/logging/Logger Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/LogRecord Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/logging/addLevelName Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/basicConfig Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/BufferingFormatter Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/captureWarnings Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/critical shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/debug shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/disable shared tags: #logging, #stdlib