| 1 |
from collections.abc import AsyncIterable |
| 2 |
|
| 3 |
from fastapi import FastAPI |
| 4 |
from fastapi.sse import EventSourceResponse, ServerSentEvent |
| 5 |
|
| 6 |
app = FastAPI() |
| 7 |
|
| 8 |
|
| 9 |
@app.get("/logs/stream", response_class=EventSourceResponse) |
| 10 |
async def stream_logs() -> AsyncIterable[ServerSentEvent]: |
| 11 |
logs = [ |
| 12 |
"2025-01-01 INFO Application started", |
| 13 |
"2025-01-01 DEBUG Connected to database", |
| 14 |
"2025-01-01 WARN High memory usage detected", |
| 15 |
] |
| 16 |
for log_line in logs: |
| 17 |
yield ServerSentEvent(raw_data=log_line) |