Skip to content

Conversation

@Gustavo-Hagenbeck
Copy link
Contributor

@Gustavo-Hagenbeck Gustavo-Hagenbeck commented Dec 8, 2025

Problem

The SDK currently uses traceback.print_exc() and traceback.print_exception() which write directly to stderr, bypassing Python's logging system. This prevents applications from:

  • Configuring log destinations (files, syslog, cloud services)
  • Controlling log levels and filtering
  • Using structured logging formats (JSON, etc.)
  • Integrating with monitoring and alerting systems

PEP 337: Logging Usage in the Standard Library explicitly states:

"Replace traceback.print_exception with _log.exception"

And recommends that modules should:

"use the logging system instead of print or sys.stdout.write"

Solution

Replaced all instances of traceback.print_exc() and traceback.print_exception() with logger.exception(), following the recommendation from PEP 337.

Changes

  • Added logging.getLogger(__name__) to server.py and server_context.py
  • Replaced traceback.print_exc() with logger.exception() in 2 locations
  • Replaced traceback.print_exception() with logger.exception() in 2 locations
  • Removed unused traceback import from server.py

Testing

All checks pass successfully:

$ uv run ruff format
53 files left unchanged

$ uv run ruff check
All checks passed!


$ PYRIGHT_PYTHON_IGNORE_WARNINGS=1 uv run pyright python/
0 errors, 0 warnings, 0 informations

$ uv run mypy --check-untyped-defs --ignore-missing-imports python/
Success: no issues found in 24 source files

$ uv run pytest tests/*.py -v
============================= test session starts ==============================
platform darwin -- Python 3.12.0, pytest-8.4.2, pluggy-1.6.0 -- /Users/gustavogomes/projects/sdk-python/.venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/gustavogomes/projects/sdk-python
configfile: pyproject.toml
plugins: anyio-4.11.0
collecting ... collected 13 items

tests/ext.py::test_greeter PASSED                                        [  7%]
tests/ext.py::test_greeter_with_cm PASSED                                [ 15%]
tests/harness.py::test_greeter PASSED                                    [ 23%]
tests/harness.py::test_counter PASSED                                    [ 30%]
tests/harness.py::test_idempotency_key PASSED                            [ 38%]
tests/harness.py::test_workflow PASSED                                   [ 46%]
tests/harness.py::test_send PASSED                                       [ 53%]
tests/serde.py::test_bytes_serde PASSED                                  [ 61%]
tests/servercontext.py::test_sanity PASSED                               [ 69%]
tests/servercontext.py::test_wrapped_terminal_exception PASSED           [ 76%]
tests/servercontext.py::test_promise_default_serde PASSED                [ 84%]
tests/servercontext.py::test_handler_with_union_none PASSED              [ 92%]
tests/servercontext.py::test_handler_with_ctx_none PASSED                [100%]

============================= 13 passed in 22.47s ==============================

Using traceback.print_exc() and traceback.print_exception() writes directly
to stderr, bypassing the Python logging system. This prevents users from:
- Configuring log destinations (files, syslog, cloud services)
- Controlling log levels and filtering
- Using structured logging formats (JSON, etc.)
- Integrating with monitoring and alerting systems

PEP 337 explicitly recommends: "Replace traceback.print_exception with
_log.exception" and states that modules should "use the logging system
instead of print or sys.stdout.write."

Changes:
- Added logging.getLogger(__name__) to server.py and server_context.py
- Replaced traceback.print_exc() with logger.exception() in 2 locations
- Replaced traceback.print_exception() with logger.exception() in 2 locations

This allows applications using the SDK to properly control exception logging
through standard Python logging configuration.

References:
- PEP 337: https://peps.python.org/pep-0337/
- Python Logging HOWTO: https://docs.python.org/3/howto/logging.html
Copy link
Contributor

@igalshilman igalshilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Gustavo-Hagenbeck, LGTM.

@igalshilman igalshilman merged commit 2261661 into restatedev:main Dec 8, 2025
6 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Dec 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants