nirs4all.core.logging.context module
Run context management for nirs4all logging.
This module provides context managers for tracking run state, phases, branches, sources, and stacking operations in the logging system.
- class nirs4all.core.logging.context.BranchContext(name: str, path: list[str] = <factory>, index: int | None = None, total: int | None = None, parent: str | None = None, depth: int = 0)[source]
Bases:
objectContext for a branch in the pipeline.
- class nirs4all.core.logging.context.LogContext(run_id: str | None = None, run_name: str | None = None, project: str | None = None, **extra: Any)[source]
Bases:
objectContext manager for run-level logging context.
Provides a context manager that sets up run state and can be used to track the current run, phase, branches, sources, etc.
Example
>>> with LogContext(run_id="my-experiment", project="protein"): ... logger.info("Starting analysis") ... with LogContext.branch("snv", index=0, total=4): ... logger.info("Processing SNV branch")
- __enter__() LogContext[source]
Enter the context, setting up run state.
- __exit__(exc_type: Any, exc_val: Any, exc_tb: Any) None[source]
Exit the context, restoring previous state.
- static branch(name: str, index: int | None = None, total: int | None = None, parent: str | None = None) Generator[BranchContext, None, None][source]
Context manager for tracking branch execution.
- Parameters:
name – Branch name.
index – Branch index (0-based).
total – Total number of branches at this level.
parent – Parent branch name (for nested branches).
- Yields:
BranchContext for the current branch.
- static phase(phase: Phase) Generator[None, None, None][source]
Context manager for tracking the current phase.
- Parameters:
phase – The phase being entered.
- Yields:
None
- static source(name: str, index: int | None = None, total: int | None = None) Generator[SourceContext, None, None][source]
Context manager for tracking source processing in multi-source pipelines.
- Parameters:
name – Source name.
index – Source index (0-based).
total – Total number of sources.
- Yields:
SourceContext for the current source.
- static stack(n_branches: int, meta_model: str | None = None, branch_sources: list[str] | None = None) Generator[StackContext, None, None][source]
Context manager for tracking stacking operations.
- Parameters:
n_branches – Number of branches being stacked.
meta_model – Meta-model name/description.
branch_sources – List of branch names being stacked.
- Yields:
StackContext for the stacking operation.
- class nirs4all.core.logging.context.RunState(run_id: str, run_name: str | None = None, project: str | None = None, start_time: ~datetime.datetime = <factory>, current_phase: ~nirs4all.core.logging.events.Phase | None = None, branch_stack: list[~nirs4all.core.logging.context.BranchContext] = <factory>, source_context: ~nirs4all.core.logging.context.SourceContext | None = None, stack_context: ~nirs4all.core.logging.context.StackContext | None = None, extra: dict[str, ~typing.Any] = <factory>)[source]
Bases:
objectState for a single run.
- start_time
Run start timestamp.
- Type:
- current_phase
Current workflow phase.
- Type:
- branch_stack
Stack of branch contexts (for nesting).
- source_context
Current source context (if any).
- Type:
- stack_context
Current stacking context (if any).
- Type:
- branch_stack: list[BranchContext]
- property current_branch: BranchContext | None
Get the current (innermost) branch context.
- source_context: SourceContext | None = None
- stack_context: StackContext | None = None
- class nirs4all.core.logging.context.SourceContext(name: str, index: int | None = None, total: int | None = None)[source]
Bases:
objectContext for a source in multi-source pipelines.
- class nirs4all.core.logging.context.StackContext(n_branches: int, meta_model: str | None = None, branch_sources: list[str] = <factory>)[source]
Bases:
objectContext for stacking operations.
- nirs4all.core.logging.context.get_current_state() RunState | None[source]
Get the current run state.
- Returns:
Current RunState or None if not in a run context.
- nirs4all.core.logging.context.get_run_id() str | None[source]
Get the current run ID.
- Returns:
Current run ID or None if not in a run context.
- nirs4all.core.logging.context.inject_context(record: LogRecord) LogRecord[source]
Inject current context into a log record.
This is called by the logger to add context fields to each log record.
- Parameters:
record – Log record to inject context into.
- Returns:
Modified log record with context fields.