From 7c30f7a7204a80dac641a48fc080e338c630aa87 Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Sun, 1 Jun 2025 20:06:45 +0200 Subject: [PATCH 1/3] Adjust simulation config to match full config updates, fix .gitignore --- .gitignore | 4 ++-- ofm_config_simulation.json | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 4f6925c9..e30a155b 100644 --- a/.gitignore +++ b/.gitignore @@ -79,8 +79,8 @@ packages.png openflexure_microscope/cobertura.xml # labthings settings -/settings/ -/openflexure_settings/ +settings/ +openflexure_settings/ # web app build /src/openflexure_microscope_server/static/ diff --git a/ofm_config_simulation.json b/ofm_config_simulation.json index 0d06ad87..42c1a356 100644 --- a/ofm_config_simulation.json +++ b/ofm_config_simulation.json @@ -8,8 +8,8 @@ "/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/" } \ No newline at end of file From 4ef74c42c3b613e62e02f537be2ce3c40844c5f3 Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Tue, 3 Jun 2025 14:03:46 +0100 Subject: [PATCH 2/3] Configure log folder in json file --- ofm_config_full.json | 3 ++- ofm_config_simulation.json | 3 ++- ofm_config_stub.json | 7 ++++--- src/openflexure_microscope_server/logging.py | 19 ++++++++++++------- .../server/__init__.py | 13 +++++-------- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/ofm_config_full.json b/ofm_config_full.json index f182a774..0e2d9cae 100644 --- a/ofm_config_full.json +++ b/ofm_config_full.json @@ -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/" } diff --git a/ofm_config_simulation.json b/ofm_config_simulation.json index 42c1a356..67721c9b 100644 --- a/ofm_config_simulation.json +++ b/ofm_config_simulation.json @@ -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/" } \ No newline at end of file diff --git a/ofm_config_stub.json b/ofm_config_stub.json index d312795e..99b51f8e 100644 --- a/ofm_config_stub.json +++ b/ofm_config_stub.json @@ -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/" } \ No newline at end of file diff --git a/src/openflexure_microscope_server/logging.py b/src/openflexure_microscope_server/logging.py index d161197d..63832652 100644 --- a/src/openflexure_microscope_server/logging.py +++ b/src/openflexure_microscope_server/logging.py @@ -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. diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index 7c938362..7a8a94e6 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -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, From c919f8a19f1c3657005333dbdb2118c0a5bbbc01 Mon Sep 17 00:00:00 2001 From: Joe Knapper Date: Tue, 3 Jun 2025 14:48:13 +0100 Subject: [PATCH 3/3] Logging config based on default uvicorn settings --- .../server/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index 7a8a94e6..bcf997a3 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -1,6 +1,7 @@ from __future__ import annotations from typing import Optional +from copy import copy from labthings_fastapi.server import cli, ThingServer import uvicorn @@ -23,6 +24,11 @@ def customise_server(server: ThingServer, log_folder: str): def serve_from_cli(argv: Optional[list[str]] = None): """Start the server from the command line""" args = cli.parse_args(argv) + + log_config = copy(uvicorn.config.LOGGING_CONFIG) + log_config["loggers"]["uvicorn"]["propagate"] = True + log_config["loggers"]["uvicorn.access"]["propagate"] = True + try: config = cli.config_from_args(args) log_folder = config.get("log_folder", "./openflexure/logs") @@ -32,10 +38,7 @@ def serve_from_cli(argv: Optional[list[str]] = None): server.app, host=args.host, port=args.port, - log_config={ - "version": 1, - "disable_existing_loggers": False, - }, + log_config=log_config, ) except BaseException as e: @@ -51,10 +54,7 @@ def serve_from_cli(argv: Optional[list[str]] = None): app, host=args.host, port=args.port, - log_config={ - "version": 1, - "disable_existing_loggers": False, - }, + log_config=log_config, ) else: raise e