httk.core.report¶
Unified reporting for the httk logger hierarchy.
Library code reports diagnostics with logging.getLogger() under
httk.*. Console/file handlers and per-task collections are consumers of
that same record stream. Context is supplied at emission sites with
extra={"context": ...}, as one string or a list of strings. A general
level and optional per-context levels control each consumer; a context level
can also demote a context by being higher than the general level.
rearm() lets a new task collect warnings already seen by an earlier
task. With no configuration or collection, importing this module changes no
logging or warnings state. Installing any handler on "httk", including
the collecting handler, suppresses logging.lastResort for this hierarchy,
so server processes using collect_reports() should also call
configure_reporting() for console output.
Attributes¶
Classes¶
Render one record as a single line of JSON. |
|
Accept records at a general or context-specific threshold. |
|
Store append-only records accepted by one |
Functions¶
|
Return the numeric level for a protocol log-level name. |
|
Return an adapter which attaches |
|
Install one console handler for a reporting logger hierarchy. |
|
Add one rotating report file handler and return the path it writes. |
|
Remove the reporting handlers installed for |
|
Collect records for the current context until the |
Return the |
|
|
Invalidate warning deduplication caches without changing warning policy. |
Module Contents¶
- class httk.core.report.JsonFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]¶
Bases:
logging.FormatterRender one record as a single line of JSON.
- format(record)[source]¶
Render
recordas one JSON line, including non-standard fields.- Parameters:
record (logging.LogRecord) – Log record to serialize.
- Returns:
JSON text containing the record’s report fields.
- Return type:
- httk.core.report.resolve_level(level)[source]¶
Return the numeric level for a protocol log-level name.
- Parameters:
level (str | int) – Numeric level or case-insensitive logging level name.
- Returns:
Numeric logging threshold.
- Raises:
ValueError – If
levelis not a recognized logging level name.- Return type:
- class httk.core.report.ReportFilter(level='warning', context_levels=None)[source]¶
Bases:
logging.FilterAccept records at a general or context-specific threshold.
A context threshold replaces the general threshold for records carrying that context, so it can deliberately demote a noisy context by using a higher level.
- Parameters:
- filter(record)[source]¶
Return whether
recordmeets its applicable reporting threshold.- Parameters:
record (logging.LogRecord) – Log record to test against the general or context level.
- Returns:
Whether the record should be handled.
- Return type:
- httk.core.report.context_logger(logger, *contexts)[source]¶
Return an adapter which attaches
contextsto every record.Passed adapters are flattened so their
extravalues survive logging’s default adapter processing. Custom adapterprocess()logic is deliberately bypassed.- Parameters:
logger (logging.Logger | logging.LoggerAdapter) – Logger or adapter whose records receive the contexts.
*contexts (str) – Context names to attach to each emitted record.
- Returns:
A logger adapter that merges these contexts with call-specific ones.
- Return type:
- httk.core.report.configure_reporting(*, level='warning', json_logs=False, context_levels=None, capture_warnings=False, logger=DEFAULT_LOGGER)[source]¶
Install one console handler for a reporting logger hierarchy.
The general level and optional per-context levels control admission to the handler. When requested, warning capture is rearmed and kept in the same logging pipeline.
- Parameters:
json_logs (bool) – Whether to render records as JSON lines.
context_levels (collections.abc.Mapping[str, str | int] | None) – Optional minimum levels for named contexts.
capture_warnings (bool) – Whether to route Python warnings through logging.
logger (str) – Logger hierarchy that receives the handler.
- Raises:
ValueError – If a supplied log level is not recognized.
- httk.core.report.add_report_file(path, *, level='info', json_logs=False, context_levels=None, maximum_bytes=DEFAULT_MAXIMUM_BYTES, backup_count=DEFAULT_BACKUP_COUNT, logger=DEFAULT_LOGGER)[source]¶
Add one rotating report file handler and return the path it writes.
- Parameters:
path (pathlib.Path) – File path for the rotating report log.
json_logs (bool) – Whether to render records as JSON lines.
context_levels (collections.abc.Mapping[str, str | int] | None) – Optional minimum levels for named contexts.
maximum_bytes (int) – Maximum size of one report file before rotation.
backup_count (int) – Number of rotated report files to retain.
logger (str) – Logger hierarchy that receives the handler.
- Returns:
The report path supplied by the caller.
- Raises:
ValueError – If a supplied log level is not recognized.
- Return type:
- httk.core.report.reset_reporting(logger=DEFAULT_LOGGER)[source]¶
Remove the reporting handlers installed for
logger.- Parameters:
logger (str) – Logger hierarchy whose handlers are reset.
- class httk.core.report.ReportCollection(report_filter)[source]¶
Store append-only records accepted by one
collect_reports()block.- Parameters:
report_filter (ReportFilter) – Threshold policy used to accept records.
- records: list[logging.LogRecord] = [][source]¶
- httk.core.report.collect_reports(level='warning', *, context_levels=None, rearm=True)[source]¶
Collect records for the current context until the
withblock exits.Warning-registry invalidation is process-global. Concurrent collection scopes can double-collect or zero-collect repeated warnings, so callers should avoid task switches inside the block or pass
rearm=False.The active collections are stored in a
contextvars.ContextVar, so nested and copied execution contexts retain their own collection scope. Entering a collection installs the shared collecting handler and captures warnings; by default it rearms warning registries first. Exiting restores the prior collection scope and warning-capture state.- Parameters:
- Returns:
A context manager yielding the collection for the current context.
- Raises:
ValueError – If a supplied log level is not recognized.
- Return type:
- httk.core.report.active_collections()[source]¶
Return the
collect_reports()collections active in this context.Outermost first; the last entry is the innermost enclosing block. Code that presents collected records (for example a server building a response) reads them from here instead of threading a collection through call signatures.
- Returns:
Active collections from the outermost to the innermost scope.
- Return type:
tuple[ReportCollection, Ellipsis]
- httk.core.report.rearm()[source]¶
Invalidate warning deduplication caches without changing warning policy.
CPython’s
warnings._filters_mutatedis the primary mechanism used bywarnings.catch_warnings. Other implementations get an identical temporary public filter entry, whose version bump is retained after it is removed. This is process-global: overlapping collection scopes in tasks can double-collect or zero-collect repeated warnings; avoid task switches in a collection block or userearm=False.