Configure log folder in json file

This commit is contained in:
Julian Stirling 2025-06-03 14:03:46 +01:00
parent 7c30f7a720
commit 4ef74c42c3
5 changed files with 25 additions and 20 deletions

View file

@ -11,5 +11,6 @@
"/background_detect/": "openflexure_microscope_server.things.background_detect:BackgroundDetectThing",
"/capture/": "openflexure_microscope_server.things.capture:CaptureThing"
},
"settings_folder": "/var/openflexure/settings/"
"settings_folder": "/var/openflexure/settings/",
"log_folder": "/var/openflexure/logs/"
}

View file

@ -11,5 +11,6 @@
"/background_detect/": "openflexure_microscope_server.things.background_detect:BackgroundDetectThing",
"/capture/": "openflexure_microscope_server.things.capture:CaptureThing"
},
"settings_folder": "./openflexure/settings/"
"settings_folder": "./openflexure/settings/",
"log_folder": "./openflexure/logs/"
}

View file

@ -8,8 +8,9 @@
"/system_control/": "openflexure_microscope_server.things.system_control:SystemControlThing",
"/settings/": "openflexure_microscope_server.things.settings_manager:SettingsManager",
"/smart_scan/": "openflexure_microscope_server.things.smart_scan:SmartScanThing",
"/background_detect/": "openflexure_microscope_server.things.smart_scan:BackgroundDetectThing",
"/api_test/": "openflexure_microscope_server.things.test:APITestThing"
"/background_detect/": "openflexure_microscope_server.things.background_detect:BackgroundDetectThing",
"/capture/": "openflexure_microscope_server.things.capture:CaptureThing"
},
"settings_folder": "./openflexure_settings/"
"settings_folder": "./openflexure/settings/",
"log_folder": "./openflexure/logs/"
}

View file

@ -4,17 +4,19 @@ import os
from fastapi.responses import PlainTextResponse
OFM_LOG_FOLDER = "/var/openflexure/logs/"
OFM_LOG_FILE = os.path.join(OFM_LOG_FOLDER, "openflexure_microscope.log")
OFM_LOG_FILE = None
def configure_logging():
def configure_logging(log_folder):
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
# Explictly make OFM_LOG_FILE a global so it can be updated based on log settings
global OFM_LOG_FILE
OFM_LOG_FILE = os.path.join(log_folder, "openflexure_microscope.log")
try:
if not os.path.exists(OFM_LOG_FOLDER):
os.makedirs(OFM_LOG_FOLDER)
if not os.path.exists(log_folder):
os.makedirs(log_folder)
handler = RotatingFileHandler(
filename=OFM_LOG_FILE,
mode="a",
@ -38,7 +40,7 @@ def configure_logging():
def retrieve_log() -> PlainTextResponse:
"""
Returns logs since we started running the server, up to a maxiumum of
Returns logs since we started running the server, up to a maximum of
250 messages. This log is the one shown in the UI and on the logging page.
It does not include any of the `uvicorn.access` logs as these are emmitted
@ -57,6 +59,10 @@ def retrieve_log_from_file() -> PlainTextResponse:
Note this is read and then sent as otherwise it causes a RuntimeError if it
is written to while sending through FileResponse
"""
if OFM_LOG_FILE is None:
raise RuntimeError(
"Cannot retrieve log file as logging directory hasn't been configured"
)
with open(OFM_LOG_FILE, "r", encoding="utf-8") as logfile:
full_log = logfile.read()
return PlainTextResponse(full_log)
@ -86,7 +92,6 @@ class OFMHandler(logging.Handler):
"""
Emit will save the logged record to the log
"""
# Basic filter for now that simply stops uvicorn.access logs
# These are the logs each time an API endpoint is accessed
# This is only the log for the UI.

View file

@ -9,14 +9,11 @@ from .legacy_api import add_v2_endpoints
from ..logging import configure_logging, retrieve_log, retrieve_log_from_file
def customise_server(server: ThingServer):
def customise_server(server: ThingServer, log_folder: str):
"""Customise the server with additional endpoints, etc."""
configure_logging()
configure_logging(log_folder)
add_v2_endpoints(server)
try:
add_static_files(server.app)
except RuntimeError:
print("Failed to add static files - you will have to do without them!")
add_static_files(server.app)
# Add an endpoint to get the logs - (directly calling the FastAPI decorator)
server.app.get("/log/")(retrieve_log)
@ -27,10 +24,10 @@ def serve_from_cli(argv: Optional[list[str]] = None):
"""Start the server from the command line"""
args = cli.parse_args(argv)
try:
config, server = None, None
config = cli.config_from_args(args)
log_folder = config.get("log_folder", "./openflexure/logs")
server = cli.server_from_config(config)
customise_server(server)
customise_server(server, log_folder)
uvicorn.run(
server.app,
host=args.host,