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

@ -22,8 +22,8 @@ To access your microscope log, either:
* Run `ofm log` on your microscope, and copy/paste the output here * Run `ofm log` on your microscope, and copy/paste the output here
* Go to `http://<your microscope IP>:5000/log`, download the log file, and attach it here using the "Attach a File" button below * Go to `http://<your microscope IP>:5000/logfile`, download the log file, and attach it here using the "Attach a File" button below
* In most setups, `http://microscope.local:5000/log` should work * In most setups, `http://microscope.local:5000/logfile` should work
## Additional details ## Additional details

View file

@ -2,7 +2,7 @@ import logging
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
import os import os
from fastapi.responses import FileResponse, PlainTextResponse from fastapi.responses import PlainTextResponse
OFM_LOG_FOLDER = "/var/openflexure/logs/" OFM_LOG_FOLDER = "/var/openflexure/logs/"
OFM_LOG_FILE = os.path.join(OFM_LOG_FOLDER, "openflexure_microscope.log") 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) 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): class OFMHandler(logging.Handler):
""" """
A child class of logging.Handler. This class handles storing the most recent 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 import uvicorn
from .serve_static_files import add_static_files from .serve_static_files import add_static_files
from .legacy_api import add_v2_endpoints 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): def customise_server(server: ThingServer):
@ -18,8 +18,9 @@ def customise_server(server: ThingServer):
except RuntimeError: except RuntimeError:
print("Failed to add static files - you will have to do without them!") 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("/log/")(retrieve_log)
server.app.get("/logfile/")(retrieve_log_from_file)
def serve_from_cli(argv: Optional[list[str]] = None): def serve_from_cli(argv: Optional[list[str]] = None):

View file

@ -33,7 +33,7 @@
<a <a
class="uk-button uk-button-default" class="uk-button uk-button-default"
:href="logFileURI" :href="logFileURI"
download="openflexure_microscope-ui.log" download="openflexure_microscope.log"
>Download Log File</a >Download Log File</a
> >
</div> </div>
@ -128,9 +128,12 @@ export default {
return items; return items;
}, },
logFileURI: function() { logURI: function() {
return `${this.$store.getters.baseUri}/log/`; return `${this.$store.getters.baseUri}/log/`;
}, },
logFileURI: function() {
return `${this.$store.getters.baseUri}/logfile/`;
},
pagedItems: function() { pagedItems: function() {
let startIndex = (this.page - 1) * this.maxitems; let startIndex = (this.page - 1) * this.maxitems;
return this.filteredItems.slice(startIndex, startIndex + this.maxitems); return this.filteredItems.slice(startIndex, startIndex + this.maxitems);
@ -153,7 +156,7 @@ export default {
} }
}, },
async updateLogs() { async updateLogs() {
let response = await axios.get(this.logFileURI); let response = await axios.get(this.logURI);
let lines = response.data.split("\n"); let lines = response.data.split("\n");
let logs = []; let logs = [];
let regexp = /\[(.+)\] \[(.+)\] (.*)$/; let regexp = /\[(.+)\] \[(.+)\] (.*)$/;