Add logger to Python SDK#2746
Conversation
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR adds structured logging support to the async Python client. A new ChangesLogging Infrastructure and Integration
Sequence DiagramsequenceDiagram
participant Caller
participant AsyncInstant
participant AsyncStreams
participant AsyncStreamReader
participant AsyncStreamWriter
Caller->>AsyncInstant: __init__(verbose=True, logger=custom)
AsyncInstant->>AsyncInstant: _log = make_logger(verbose, logger)
AsyncInstant->>AsyncStreams: __init__(log=_log)
AsyncStreams->>AsyncStreamReader: __init__(log=_log)
AsyncStreams->>AsyncStreamWriter: __init__(log=_log)
AsyncInstant->>AsyncInstant: subscribe_query(log=_log)
🎯 3 (Moderate) | ⏱️ ~25 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@client/packages/python/src/instantdb/_async/streams/_connection.py`:
- Line 158: The debug log lines currently force message stringification
(f"[send] {event_id} {msg}" and similar at the receive path) even when debug
logging is disabled; update the calls in _connection.py to avoid eager
formatting by either wrapping them with a debug-level guard (e.g., if
self._log.isEnabledFor(logging.DEBUG): self._log.debug(f"...")) or by using lazy
formatting via the logger's formatting API (e.g., self._log.debug("[send] %s
%s", event_id, msg)); apply the same change for the corresponding receive log at
the second occurrence so both hot-path send/receive avoid unnecessary
serialization.
In `@client/packages/python/src/instantdb/_async/subscribe.py`:
- Line 145: The debug calls in subscribe (e.g., the f-string at
self._log.debug(f"[receive] {msg}") and the similar one around line 187) eagerly
format strings even when debug is disabled; wrap those debug log calls in a
guard like if self._log.isEnabledFor(logging.DEBUG): ... or switch to logger
lazy formatting (pass format and args instead of an f-string) so the expensive
payload/error string formatting only happens when DEBUG is enabled; update the
occurrences at the debug calls (self._log.debug) in the subscribe/_async logic
accordingly.
In `@client/packages/python/tests/test_logger.py`:
- Around line 82-90: The test test_stream_connection_logs_received_messages
creates an _AsyncHTTP instance but never closes it, causing a client leak;
update the test to ensure the _AsyncHTTP created for _AsyncStreamConnection is
closed (use a try/finally or async context) — locate where _AsyncHTTP(...) is
constructed for _AsyncStreamConnection in the test, wrap the connection usage in
a try/finally and call await http_instance.aclose() (or the instance used in
_AsyncStreamConnection) in the finally so the HTTP client is properly closed
after conn._dispatch.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 3ef1af57-5bdd-4db3-a33f-516b13dc9643
📒 Files selected for processing (11)
client/packages/python/src/instantdb/__init__.pyclient/packages/python/src/instantdb/_async/client.pyclient/packages/python/src/instantdb/_async/streams/__init__.pyclient/packages/python/src/instantdb/_async/streams/_connection.pyclient/packages/python/src/instantdb/_async/streams/reader.pyclient/packages/python/src/instantdb/_async/streams/writer.pyclient/packages/python/src/instantdb/_async/subscribe.pyclient/packages/python/src/instantdb/_logger.pyclient/packages/python/tests/test_logger.pyclient/www/app/docs/init/page.mdclient/www/app/docs/start-python/page.md
|
View Vercel preview at instant-www-js-add-logger-to-python-sdk-jsv.vercel.app. |
- Skip building [send]/[receive] log strings when logging is disabled, so a verbose=False client never serializes message payloads on the hot path - Close _AsyncHTTP in the stream-connection logger test - Add a regression test that the disabled hot path skips payload formatting - Drop redundant private-attribute asserts from the clone test Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Follow up to #2745, adds ability to add logger to Python SDK matching JS admin sdk behavior.
Also updates docs to include a note about the
loggerconfig