Logging¶
Structured logging utilities built on structlog.
configure_logging
¶
configure_logging(
level: LogLevel = LogLevel.INFO,
format: LogFormat = LogFormat.JSON,
stream: Any | None = None,
) -> None
Configure structured logging for the application.
This should be called once at application startup. Subsequent calls will reconfigure logging (useful for testing).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
LogLevel
|
The minimum log level to output |
INFO
|
format
|
LogFormat
|
The output format (JSON for production, CONSOLE for development) |
JSON
|
stream
|
Any | None
|
Output stream (defaults to sys.stdout) |
None
|
get_logger
¶
Get a structured logger for the given module name.
This returns a structlog BoundLogger that wraps the stdlib logger. If logging hasn't been configured, it will use sensible defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The logger name (typically name) |
required |
Returns:
| Type | Description |
|---|---|
BoundLogger
|
A structured logger instance |
LogLevel
¶
Bases: StrEnum
Log level enumeration.
LogFormat
¶
Bases: StrEnum
Log format enumeration.
Context Management¶
add_context
¶
Add persistent context to all subsequent log messages.
Context is stored in a context variable and automatically added to all log messages. Use this for values like correlation_id or deployment_id that should appear in all logs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Key-value pairs to add to context |
{}
|
clear_context
¶
with_context
¶
Context manager for temporary logging context.
Adds context for the duration of a block, then removes it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Key-value pairs to add temporarily |
{}
|
Example
Returns:
| Type | Description |
|---|---|
AbstractContextManager[None]
|
Context manager |