logging_middleware.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """Logging middleware."""
  2. from __future__ import annotations
  3. from typing import TYPE_CHECKING
  4. from pynecone.event import Event
  5. from pynecone.middleware.middleware import Middleware
  6. from pynecone.state import State, StateUpdate
  7. if TYPE_CHECKING:
  8. from pynecone.app import App
  9. class LoggingMiddleware(Middleware):
  10. """Middleware to log requests and responses."""
  11. async def preprocess(self, app: App, state: State, event: Event):
  12. """Preprocess the event.
  13. Args:
  14. app: The app to apply the middleware to.
  15. state: The client state.
  16. event: The event to preprocess.
  17. """
  18. print(f"Event {event}")
  19. async def postprocess(
  20. self, app: App, state: State, event: Event, update: StateUpdate
  21. ):
  22. """Postprocess the event.
  23. Args:
  24. app: The app to apply the middleware to.
  25. state: The client state.
  26. event: The event to postprocess.
  27. update: The current state update.
  28. """
  29. print(f"Update {update}")