logging
azure_bootstrap.logging
¶
Tier 1 logging primitives.
Always-on, stdlib-only. The top-level azure_bootstrap package re-exports
the most common entry-points (configure_logging, correlation_scope,
mask_*). Deeper imports stay available for callers that want fine-grained
access.
Modules:
| Name | Description |
|---|---|
config |
Single entry-point for v2 logging setup. |
correlation |
Correlation context for structured logs. |
formatter |
v2 log formatter: appends extra={} fields as |
jsonformatter |
v2.1 JSON log formatter — one JSON object per record. |
masking |
Secret/PII masking and log-injection sanitization primitives. |
noise |
Silence chatty third-party loggers. |
Classes:
| Name | Description |
|---|---|
CorrelationFilter |
Attach every set context var as a record attribute. |
ExtraFieldsFormatter |
Render structured extras after the base format string. |
LoggingExtraConflictError |
Raised in DEBUG when a caller's |
JsonLogFormatter |
Render a log record as a single line of JSON. |
Functions:
| Name | Description |
|---|---|
configure_logging |
Install structured-logging defaults. Idempotent — replaces handlers. |
debug_logging_enabled |
Second-factor gate on DEBUG output. |
correlation_scope |
Push correlation context for the duration of the with-block. |
mask_secrets_in_dict |
Shallow copy with secret-keyed values replaced by '***'. |
register_secret_keys |
Extend the allowlist at runtime. Names are lowercased. |
sanitize_for_log |
Replace control chars (\x00-\x1f, \x7f) with '?' and truncate. |
register_noisy_logger |
Append a logger name to the default registry for the lifetime of the process. |
silence_noisy_loggers |
Clamp the named loggers to |
CorrelationFilter
¶
Bases: Filter
Attach every set context var as a record attribute.
Only sets attributes the record doesn't already carry — so explicit
extra={"correlation_id": "..."} overrides win.
ExtraFieldsFormatter
¶
ExtraFieldsFormatter(fmt: str | None = '%(asctime)s %(levelname)s %(name)s %(message)s', datefmt: str | None = None, style: str = '%', validate: bool = True)
Bases: Formatter
Render structured extras after the base format string.
Output: <base> key1='value1' key2=42 key3=<MyObj>. Two-space gap is
intentional so grep ' key=' finds field hits without matching the
message body.
Source code in azure_bootstrap/logging/formatter.py
LoggingExtraConflictError
¶
Bases: Exception
Raised in DEBUG when a caller's extra={} collides with a reserved key.
JsonLogFormatter
¶
Bases: Formatter
Render a log record as a single line of JSON.
Emitted fields: timestamp (ISO-8601 UTC), level, logger,
message, exception (only when exc_info is present), plus every
non-reserved extra={} field (correlation IDs included, since
:class:CorrelationFilter attaches them as record attributes). Extra fields
are passed through :func:mask_secrets_in_dict so secret-keyed values are
redacted to ***.
Source code in azure_bootstrap/logging/jsonformatter.py
configure_logging
¶
configure_logging(*, format_string: str = '%(asctime)s %(levelname)s %(name)s %(message)s', silence_defaults: bool = True, extra_noisy_loggers: tuple[str, ...] = ()) -> None
Install structured-logging defaults. Idempotent — replaces handlers.
Source code in azure_bootstrap/logging/config.py
debug_logging_enabled
¶
debug_logging_enabled() -> bool
Second-factor gate on DEBUG output.
LOG_LEVEL=DEBUG alone is not enough — DEBUG_LOGGING_ENABLED must
also be truthy. Belt-and-suspenders against a stray manifest leaking
DEBUG into prod.
Source code in azure_bootstrap/logging/config.py
correlation_scope
¶
correlation_scope(correlation_id: str | None = None, **fields: str | None) -> Generator[str, None, None]
Push correlation context for the duration of the with-block.
Yields the resolved correlation_id, always a non-empty string. A fresh
12-char uuid hex is minted when correlation_id is None. Any keyword
argument becomes a context var (e.g. email_id, request_id).
Source code in azure_bootstrap/logging/correlation.py
mask_secrets_in_dict
¶
Shallow copy with secret-keyed values replaced by '***'.
A value is replaced only when truthy — empty strings, None, 0, etc. pass through unmodified so the caller can still see "field was empty".
Source code in azure_bootstrap/logging/masking.py
sanitize_for_log
¶
Replace control chars (\x00-\x1f, \x7f) with '?' and truncate.
Source code in azure_bootstrap/logging/masking.py
register_noisy_logger
¶
register_noisy_logger(name: str) -> None
Append a logger name to the default registry for the lifetime of the process.
silence_noisy_loggers
¶
Clamp the named loggers to level.
Only the named loggers are affected — root is never touched, so callers can leave root at DEBUG while these stay quiet.