logging_middleware.py 1022 B

123456789101112131415161718192021222324252627282930313233343536
  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 Delta, State
  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(self, app: App, state: State, event: Event, delta: Delta):
  20. """Postprocess the event.
  21. Args:
  22. app: The app to apply the middleware to.
  23. state: The client state.
  24. event: The event to postprocess.
  25. delta: The delta to postprocess.
  26. """
  27. print(f"Delta {delta}")