From bfa5183c301d368b0da9582ea3185453e6848f7a Mon Sep 17 00:00:00 2001 From: jaknapper Date: Thu, 9 Jul 2026 11:05:34 +0100 Subject: [PATCH] Format log history for fallback server --- src/openflexure_microscope_server/logging.py | 12 ++++++++++++ src/openflexure_microscope_server/server/__init__.py | 4 +--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/openflexure_microscope_server/logging.py b/src/openflexure_microscope_server/logging.py index 47c767ff..56b1ef67 100644 --- a/src/openflexure_microscope_server/logging.py +++ b/src/openflexure_microscope_server/logging.py @@ -207,6 +207,18 @@ class OFMHandler(logging.Handler): """ return list(reversed(self._log)) + @property + def get_formatted_log_history(self) -> str: + """Return the log history as a formatted string. + + This is to be used for the fallback server, the formatting does not contain invocation + IDs as no actions should have started. + """ + output = "" + for entry in self._log: + output += f"[{entry['timestamp']} [{entry['level']}] <{entry['logger']}> {entry['message']}\n" + return output + class UvicornAccessFilter(logging.Filter): """A logging filter to filter out "uvicorn.access" messages.""" diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index e9fa24bd..55a06bb6 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -2,7 +2,6 @@ from __future__ import annotations -import json import logging import os from argparse import Namespace @@ -151,8 +150,7 @@ def serve_from_cli(argv: Optional[list[str]] = None) -> None: print(f"Error: {e}") # noqa: T201 print("Starting fallback server.") # noqa: T201 try: - # The log is a list of dicts, fallback server expects a string - log_history = json.dumps(OFM_HANDLER.log_history, indent=2) + log_history = OFM_HANDLER.get_formatted_log_history except BaseException: # If log history fails for any reason carry on. log_history = None