windlass.core.logging¶
logging
¶
Logging for Windlass.
Library rule number one: do not configure the root logger. Windlass attaches
a :class:logging.NullHandler to its own logger and otherwise stays out of the
way, so an application's logging setup wins. :func:configure_logging is opt-in
and exists purely for scripts and for WINDLASS_LOG_LEVEL=DEBUG debugging.
Every logger is namespaced under windlass.*, so one line silences the whole
framework::
logging.getLogger("windlass").setLevel(logging.WARNING)
get_logger
¶
Return a Windlass-namespaced logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Usually |
required |
Returns:
| Type | Description |
|---|---|
Logger
|
A configured :class: |
Example
get_logger("windlass.core.registry").name 'windlass.core.registry' get_logger("my_plugin").name 'windlass.my_plugin'
Source code in src\windlass\core\logging.py
configure_logging
¶
configure_logging(
level: int | str = "INFO",
*,
stream: Any = None,
fmt: str | None = None,
force: bool = False
) -> logging.Logger
Attach a human-readable stream handler to the windlass logger.
Intended for scripts, notebooks and the CLI. Applications with their own logging configuration should not call this.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int | str
|
Log level name or numeric value. |
'INFO'
|
stream
|
Any
|
Target stream. Defaults to |
None
|
fmt
|
str | None
|
Custom :mod: |
None
|
force
|
bool
|
Replace handlers that a previous call installed. |
False
|
Returns:
| Type | Description |
|---|---|
Logger
|
The configured |
Example
logger = configure_logging("WARNING") logger.name 'windlass'
Source code in src\windlass\core\logging.py
bind_context
¶
Bind correlation fields for the duration of the block.
Fields nest, are async-safe (backed by :mod:contextvars) and appear on
every Windlass log record emitted inside the block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**fields
|
Any
|
Key/value pairs such as |
{}
|
Yields:
| Type | Description |
|---|---|
dict[str, Any]
|
The merged context dictionary. |
Example
with bind_context(request_id="abc"): ... current_context()["request_id"] 'abc'
Source code in src\windlass\core\logging.py
current_context
¶
log_duration
¶
Log how long a block took.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Where to emit the message. |
required |
message
|
str
|
Description of the work, e.g. |
required |
level
|
int
|
Level for the completion message. |
DEBUG
|
Yields:
| Type | Description |
|---|---|
None
|
None. |
Example
import logging with log_duration(get_logger("windlass.demo"), "work"): ... pass