From 1a13cbe5926ca55f1093fc437e01d2ec5e4ccfbf Mon Sep 17 00:00:00 2001 From: Beth Probert Date: Wed, 4 Mar 2026 17:49:48 +0000 Subject: [PATCH 1/9] Add labthings debug config when spinning up a server --- src/openflexure_microscope_server/server/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index 71ef6362..e9a5aeed 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -115,6 +115,8 @@ def serve_from_cli(argv: Optional[list[str]] = None) -> None: lt_config, internal_config = _full_config_from_args(args) server = lt.ThingServer.from_config(lt_config) + if args.debug: + lt.logs.configure_thing_logger(logging.DEBUG) customise_server( server, internal_config["log_folder"], internal_config["scans_folder"] ) From 7c6397083cd90978e93b95b957dd21a9ae749a51 Mon Sep 17 00:00:00 2001 From: Beth Probert Date: Wed, 4 Mar 2026 18:11:57 +0000 Subject: [PATCH 2/9] Add comment --- src/openflexure_microscope_server/server/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index e9a5aeed..e7b23ab8 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -115,6 +115,9 @@ def serve_from_cli(argv: Optional[list[str]] = None) -> None: lt_config, internal_config = _full_config_from_args(args) server = lt.ThingServer.from_config(lt_config) + + # If we want debug logging from labthings, the --debug argument + # must be used when starting the server. if args.debug: lt.logs.configure_thing_logger(logging.DEBUG) customise_server( From 6a8e1198d9c3a8bf6eeb537b4f28b63292f5ca0f Mon Sep 17 00:00:00 2001 From: Beth Probert Date: Wed, 4 Mar 2026 18:16:22 +0000 Subject: [PATCH 3/9] Add unit test for debug cli arg --- tests/unit_tests/test_server_cli.py | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/unit_tests/test_server_cli.py b/tests/unit_tests/test_server_cli.py index 7901fc69..06102e3c 100644 --- a/tests/unit_tests/test_server_cli.py +++ b/tests/unit_tests/test_server_cli.py @@ -103,3 +103,34 @@ def test_failed_customise(mocker): assert isinstance(fallback_app, FastAPI) # An that it has the error to display assert str(fallback_app._context.error) == "Can't touch this" + + +def test_debug_mode(mocker): + """Test that --debug flag triggers lt.logs.configure_thing_logger.""" + # Mock the LabThings function that returns the server + mocker.patch( + "openflexure_microscope_server.server.lt.ThingServer.from_config", + return_value=mocker.Mock(), + ) + # Mock customisation to avoid side effects + mocker.patch.object(ofm_server, "customise_server") + # Mock uvicorn.run to avoid starting a server + mocker.patch("openflexure_microscope_server.server.uvicorn.run") + + # Mock the target function + mock_configure_thing_logger = mocker.patch( + "openflexure_microscope_server.server.lt.logs.configure_thing_logger" + ) + + # 1. Run with --debug + ofm_server.serve_from_cli(["-c", SIM_CONFIG, "--debug"]) + + # Verify it was called with DEBUG level + mock_configure_thing_logger.assert_called_once_with(logging.DEBUG) + + # 2. Run without --debug + mock_configure_thing_logger.reset_mock() + ofm_server.serve_from_cli(["-c", SIM_CONFIG]) + + # Verify it was NOT called + mock_configure_thing_logger.assert_not_called() From b9e9e292108a14cade7a83606d8f4f49682dbbcb Mon Sep 17 00:00:00 2001 From: Beth Probert Date: Thu, 5 Mar 2026 10:38:10 +0000 Subject: [PATCH 4/9] Moving debug to customise_server --- src/openflexure_microscope_server/server/__init__.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index 137863bb..bf73dd69 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -72,9 +72,10 @@ def set_shutdown_function(shutdown_function: Callable[[], None]) -> None: def customise_server( - server: lt.ThingServer, application_config: OFMApplicationData + server: lt.ThingServer, application_config: OFMApplicationData, + debug: bool ) -> None: - """Customise the server with additional endpoints, etc.""" + """Customise the server with additional endpoints, debug mode etc.""" if DEVELOPER_MODE: # Allow CORS in developer mode for easier testing with the webapp server.app.add_middleware( @@ -88,6 +89,10 @@ def customise_server( add_v2_endpoints(server) add_static_files(server.app, application_config.data_folder) + # Configure logging to DEBUG if requested in CLI args. + if debug: + lt.logs.configure_thing_logger(logging.DEBUG) + # 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) @@ -114,7 +119,8 @@ def serve_from_cli(argv: Optional[list[str]] = None) -> None: configure_logging(application_config.log_folder) server = lt.ThingServer.from_config(lt_config) - customise_server(server, application_config) + debug = True if args.debug else False + customise_server(server, application_config, debug) def shutdown_call() -> None: try: From 417da8d1d79a00c5c5a24c2c1d1f802e24859b08 Mon Sep 17 00:00:00 2001 From: Beth Probert Date: Thu, 5 Mar 2026 10:50:24 +0000 Subject: [PATCH 5/9] Ruff fix --- src/openflexure_microscope_server/server/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index bf73dd69..11867f1a 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -119,7 +119,7 @@ def serve_from_cli(argv: Optional[list[str]] = None) -> None: configure_logging(application_config.log_folder) server = lt.ThingServer.from_config(lt_config) - debug = True if args.debug else False + debug = bool(args.debug) customise_server(server, application_config, debug) def shutdown_call() -> None: From cea1a7a7484123bdb7ad155413682fdefeaadc64 Mon Sep 17 00:00:00 2001 From: Beth Probert Date: Thu, 5 Mar 2026 11:40:25 +0000 Subject: [PATCH 6/9] Ruff fix --- src/openflexure_microscope_server/server/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index 11867f1a..ef49be4c 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -72,8 +72,7 @@ def set_shutdown_function(shutdown_function: Callable[[], None]) -> None: def customise_server( - server: lt.ThingServer, application_config: OFMApplicationData, - debug: bool + server: lt.ThingServer, application_config: OFMApplicationData, debug: bool ) -> None: """Customise the server with additional endpoints, debug mode etc.""" if DEVELOPER_MODE: From 54b656952fe0388bd21ab658d36690f4ab372add Mon Sep 17 00:00:00 2001 From: Beth Probert Date: Thu, 2 Apr 2026 15:09:39 +0100 Subject: [PATCH 7/9] Remove mock call to replace with real call --- tests/unit_tests/test_server_cli.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/unit_tests/test_server_cli.py b/tests/unit_tests/test_server_cli.py index 0f0bdec2..0e7b3ef7 100644 --- a/tests/unit_tests/test_server_cli.py +++ b/tests/unit_tests/test_server_cli.py @@ -117,8 +117,7 @@ def test_debug_mode(mocker): "openflexure_microscope_server.server.lt.ThingServer.from_config", return_value=mocker.Mock(), ) - # Mock customisation to avoid side effects - mocker.patch.object(ofm_server, "customise_server") + # Mock uvicorn.run to avoid starting a server mocker.patch("openflexure_microscope_server.server.uvicorn.run") From 7106ce13028652235ff85a42fc6bb8b042d20f0e Mon Sep 17 00:00:00 2001 From: Beth Probert Date: Thu, 2 Apr 2026 15:09:55 +0100 Subject: [PATCH 8/9] Set debug default value to False --- src/openflexure_microscope_server/server/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index ef49be4c..754a7799 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -72,7 +72,7 @@ def set_shutdown_function(shutdown_function: Callable[[], None]) -> None: def customise_server( - server: lt.ThingServer, application_config: OFMApplicationData, debug: bool + server: lt.ThingServer, application_config: OFMApplicationData, debug: bool = False ) -> None: """Customise the server with additional endpoints, debug mode etc.""" if DEVELOPER_MODE: From fff965dab4d6959d659cf50d7aa35d0fa1fc5301 Mon Sep 17 00:00:00 2001 From: Beth Probert Date: Thu, 2 Apr 2026 15:30:26 +0100 Subject: [PATCH 9/9] Fix side effects in mocking --- tests/unit_tests/test_server_cli.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/unit_tests/test_server_cli.py b/tests/unit_tests/test_server_cli.py index 0e7b3ef7..4c71a1c3 100644 --- a/tests/unit_tests/test_server_cli.py +++ b/tests/unit_tests/test_server_cli.py @@ -117,7 +117,13 @@ def test_debug_mode(mocker): "openflexure_microscope_server.server.lt.ThingServer.from_config", return_value=mocker.Mock(), ) - + # Mock customisation dependencies to avoid side effects + mocker.patch.object(ofm_server, "add_v2_endpoints") + mocker.patch.object(ofm_server, "add_static_files") + # Mock logging to avoid side effects and allow checking calls + mocker.patch.object(ofm_server, "configure_logging") + mocker.patch.object(ofm_server, "retrieve_log") + mocker.patch.object(ofm_server, "retrieve_log_from_file") # Mock uvicorn.run to avoid starting a server mocker.patch("openflexure_microscope_server.server.uvicorn.run")