Serve logfile and logs for UI seperately

This commit is contained in:
Julian Stirling 2025-04-07 23:38:09 +01:00
parent 70d9e2f758
commit 3a47151694
4 changed files with 24 additions and 8 deletions

View file

@ -2,7 +2,7 @@ import logging
from logging.handlers import RotatingFileHandler
import os
from fastapi.responses import FileResponse, PlainTextResponse
from fastapi.responses import PlainTextResponse
OFM_LOG_FOLDER = "/var/openflexure/logs/"
OFM_LOG_FILE = os.path.join(OFM_LOG_FOLDER, "openflexure_microscope.log")
@ -50,6 +50,18 @@ def retrieve_log() -> PlainTextResponse:
return PlainTextResponse(OFM_HANDLER.log_history)
def retrieve_log_from_file() -> PlainTextResponse:
"""
Returns the full log from the file for downloading.
Note this is read and then sent as otherwise it causes a RuntimeError if it
is written to while sending through FileResponse
"""
with open(OFM_LOG_FILE, "r", encoding="utf-8") as logfile:
full_log = logfile.read()
return PlainTextResponse(full_log)
class OFMHandler(logging.Handler):
"""
A child class of logging.Handler. This class handles storing the most recent

View file

@ -6,7 +6,7 @@ from labthings_fastapi.server import cli, ThingServer
import uvicorn
from .serve_static_files import add_static_files
from .legacy_api import add_v2_endpoints
from ..logging import configure_logging, retrieve_log
from ..logging import configure_logging, retrieve_log, retrieve_log_from_file
def customise_server(server: ThingServer):
@ -18,8 +18,9 @@ def customise_server(server: ThingServer):
except RuntimeError:
print("Failed to add static files - you will have to do without them!")
# Add an endpoint to get the log
# Add an endpoint to get the logs - (directly calling the FastAPI decorator)
server.app.get("/log/")(retrieve_log)
server.app.get("/logfile/")(retrieve_log_from_file)
def serve_from_cli(argv: Optional[list[str]] = None):