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