CKTIS / Python
LoggerAdapter.info — contextual logging
LoggerAdapter.info logs an INFO message with adapter context, such as a request identifier. Learn how default extra handling differs from merge_extra=True.
Version: Python 3.14; repository image, production runtime not attested
Syntax
adapter = logging.LoggerAdapter(logger, extra=None, merge_extra=False)
adapter.info(msg, *args, **kwargs)LoggerAdapter wraps a logger and adds context without repeating it at every call. LoggerAdapter.info(msg, *args, **kwargs) corresponds to Logger.info: it logs an INFO message, subject to logging configuration.
The process() method passes context as extra. Its fields become LogRecord attributes that a Formatter can display, for example using %(request_id)s. Pass message arguments separately: adapter.info('Processed %s items', 3).
The default merge_extra=False ignores extra supplied to an individual call. Setting merge_extra=True, available since Python 3.13, combines both sets; call-specific values take precedence. The example demonstrates both behaviors.
Keys in extra must not clash with reserved logging attributes. If a field required by a handler’s formatter is missing, formatting fails and that handler does not emit the record. Another handler whose formatter does not require that field can still emit the same record. The adapter neither configures handlers nor bypasses logging level thresholds.
Examples
import io
import logging
stream = io.StringIO()
handler = logging.StreamHandler(stream)
handler.setFormatter(logging.Formatter(
'%(levelname)s %(request_id)s %(message)s'
))
logger = logging.Logger('adapter-demo', level=logging.INFO)
logger.addHandler(handler)
adapter = logging.LoggerAdapter(logger, {'request_id': 'R1'})
adapter.info('Processed %s items', 3)
adapter.info('Default context', extra={'request_id': 'R2'})
merged = logging.LoggerAdapter(
logger, {'request_id': 'R1'}, merge_extra=True
)
merged.info('Merged context', extra={'request_id': 'R2'})
print(stream.getvalue(), end='')Result
INFO R1 Processed 3 items INFO R1 Default context INFO R2 Merged context
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.