trading_agent.gateway.token_redacting_filter
¶
A logging filter that hides tokens in the addresses that uvicorn and httpx log.
uvicorn logs the full address of every request, and a WebSocket carries its access token in the address as ?token=.... httpx, which python-telegram-bot uses, logs every request too, and Telegram's addresses carry the bot token as /bot<token>/. This filter replaces both before the line is written.
Typical usage example:
TokenRedactingFilter().install()
TokenRedactingFilter
¶
Bases: Filter
Rewrites a log record so that no token= value survives.
filter
¶
Replaces token values in the record's message and arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The record about to be written. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Always True, so that the record is still written. |
Source code in src/trading_agent/gateway/token_redacting_filter.py
redact
¶
Masks every access token and bot token in a piece of text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text with |
Source code in src/trading_agent/gateway/token_redacting_filter.py
install
¶
Adds this filter to uvicorn's, httpx's and python-telegram-bot's loggers.
It is also added to the root logger's handlers, because a logger's own filter does not see records from its child loggers, such as telegram.bot. Call it after uvicorn has configured its logging, because configuring replaces the loggers' handlers.