Add debug test

Add info log message for sanity

Add more useful logs

Edit configure_logging to change OFM level to debug if set in CLI

Update log message

Fix boot and int mix up

Fix string

Change string

Update logging string again

try changing OFM handler level

Change log slice

Fix syntax error

Add in OFMHandler logic, update webapp to show in logging tab and fix ruff
This commit is contained in:
Beth Probert 2026-06-17 13:02:05 +01:00
parent b235de39f6
commit b1202723a1
4 changed files with 19 additions and 12 deletions

View file

@ -23,17 +23,26 @@ LOGGER = logging.getLogger(__name__)
OFM_LOG_FILE: Optional[str] = None
def configure_logging(log_folder: str) -> None:
def configure_logging(log_folder: str, debug: bool = False) -> None:
"""Configure logging for the server while it is running.
This modifies the root logger to have a rotating file handler and
Params:
- log_folder: This modifies the root logger to have a rotating file handler and
adds a custom handler that prints and stores all records except
``uvicorn.access`` logs.
- debug: This modifies the root logger to change its logging level. It is
set to True by starting the server with the cli argument `--debug`.
It is important not to let Uvicorn override these settings.
"""
root_logger = logging.getLogger()
if debug:
root_logger.setLevel(logging.DEBUG)
else:
root_logger.setLevel(logging.INFO)
# Explicitly make OFM_LOG_FILE a global so it can be updated based on log settings
# This requires silencing PLW0603 which disallows globals.
global OFM_LOG_FILE # noqa: PLW0603
@ -44,6 +53,7 @@ def configure_logging(log_folder: str) -> None:
ofm_format_str = "[%(asctime)s] [%(levelname)s] %(message)s"
OFM_HANDLER.setFormatter(logging.Formatter(ofm_format_str))
OFM_HANDLER.addFilter(UvicornAccessFilter())
OFM_HANDLER.level = root_logger.level
root_logger.addHandler(OFM_HANDLER)
try:
@ -63,7 +73,7 @@ def configure_logging(log_folder: str) -> None:
except PermissionError as e:
LOGGER.error(f"Cannot create log file at {OFM_LOG_FILE}: {e}")
LOGGER.info("OFM server root logger has been set up at INFO level")
LOGGER.info("OFM server root logger has been set up at %s level", logging.getLevelName(root_logger.level))
def retrieve_log() -> PlainTextResponse:

View file

@ -88,11 +88,6 @@ def customise_server(
add_v2_endpoints(server)
add_static_files(server, application_config.data_folder)
# Configure logging to DEBUG if LT server is set up
# with debug = true
if server.debug:
lt.logs.configure_thing_logger(logging.DEBUG)
# Add an endpoint to get the logs - (directly calling the FastAPI decorator)
server.app.get(str(server.api_prefix.rstrip("/")) + "/log/")(retrieve_log)
server.app.get(str(server.api_prefix.rstrip("/")) + "/logfile/")(
@ -114,13 +109,14 @@ def serve_from_cli(argv: Optional[list[str]] = None) -> None:
server = None
try:
lt_config = _full_config_from_args(args)
debug = bool(args.debug)
# Validate our application data
if lt_config.application_config is None:
raise ValueError("No application configuration was supplied.")
application_config = OFMApplicationData(**lt_config.application_config)
configure_logging(application_config.log_folder)
configure_logging(application_config.log_folder, debug)
server = lt.ThingServer.from_config(lt_config, args.debug)
server = lt.ThingServer.from_config(lt_config, debug)
customise_server(server, application_config)
def shutdown_call() -> None:

View file

@ -109,6 +109,7 @@ def test_failed_customise(mocker):
# 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."""

View file

@ -121,7 +121,7 @@ export default {
...mapState(useSettingsStore, ["baseUri"]),
filteredLevels: function () {
let cutoffIndex = this.allLevels.indexOf(this.filterLevel);
return this.allLevels.slice(cutoffIndex, -1);
return this.allLevels.slice(cutoffIndex);
},
filteredItems: function () {
var items = [];