feat: send logs to UI
Added SSE endpoint `/logs/stream` for the UI to listen to logs. ref: N25B-242
This commit is contained in:
33
src/control_backend/api/v1/endpoints/logs.py
Normal file
33
src/control_backend/api/v1/endpoints/logs.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import logging
|
||||
|
||||
import zmq
|
||||
from fastapi import APIRouter
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pyjabber.server_parameters import json
|
||||
from zmq.asyncio import Context
|
||||
|
||||
from control_backend.core.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/logs/stream")
|
||||
async def log_stream():
|
||||
context = Context.instance()
|
||||
socket = context.socket(zmq.SUB)
|
||||
|
||||
for level in logging.getLevelNamesMapping():
|
||||
socket.subscribe(topic=level)
|
||||
|
||||
socket.connect(settings.zmq_settings.internal_sub_address)
|
||||
|
||||
async def gen():
|
||||
while True:
|
||||
_, message = await socket.recv_multipart()
|
||||
message = message.decode().strip()
|
||||
json_data = json.dumps(message)
|
||||
yield f"data: {json_data}\n\n"
|
||||
|
||||
return StreamingResponse(gen(), media_type="text/event-stream")
|
||||
@@ -1,6 +1,6 @@
|
||||
from fastapi.routing import APIRouter
|
||||
|
||||
from control_backend.api.v1.endpoints import command, message, sse
|
||||
from control_backend.api.v1.endpoints import command, logs, message, sse
|
||||
|
||||
api_router = APIRouter()
|
||||
|
||||
@@ -9,3 +9,5 @@ api_router.include_router(message.router, tags=["Messages"])
|
||||
api_router.include_router(sse.router, tags=["SSE"])
|
||||
|
||||
api_router.include_router(command.router, tags=["Commands"])
|
||||
|
||||
api_router.include_router(logs.router, tags=["Logs"])
|
||||
|
||||
Reference in New Issue
Block a user