Skip to content

Commit

Permalink
chore(ws-tutorial): use logging instead of print
Browse files Browse the repository at this point in the history
  • Loading branch information
CaselIT committed Jul 25, 2024
1 parent 64629f7 commit 4226ddd
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions examples/wslook/wslook/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
import logging

import uvicorn

Expand All @@ -7,6 +8,12 @@
from falcon.asgi import Request
from falcon.asgi import WebSocket


logger = logging.getLogger('ws-logger')
logger.setLevel('INFO')
logger.addHandler(logging.StreamHandler())


REPORTS = {
'report1': {
'title': 'Report 1',
Expand Down Expand Up @@ -39,7 +46,7 @@ async def process_resource_ws(self, req: Request, ws: WebSocket, resource, param
# This will be called for the HTTP request that initiates the
# WebSocket handshake after routing (if a route matches the
# request).
print(f'WebSocket connection established on {req.path}')
logger.info('WebSocket connection established on %r', req.path)


class AuthMiddleware:
Expand All @@ -64,7 +71,7 @@ async def process_request_ws(self, req: Request, ws: WebSocket):
return

# Never log tokens in production
print(f'Client with token "{token}" Authenticated')
logger.info('Client with token %r Authenticated', token)


class HelloWorldResource:
Expand All @@ -90,7 +97,7 @@ async def on_websocket(self, req: Request, ws: WebSocket):
try:
query = await ws.receive_text()
report = REPORTS.get(query, None)
print(report)
logger.info('selected report: %s', report)

if report is None:
await ws.send_media({'error': 'report not found'})
Expand Down

0 comments on commit 4226ddd

Please sign in to comment.