Store logs as dict on server, and send as json, rather than regex from str
Closes #321
This commit is contained in:
parent
330db19827
commit
6b67078129
3 changed files with 43 additions and 67 deletions
|
|
@ -1,5 +1,6 @@
|
|||
"""Test the server's logging configuration and handling."""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
|
|
@ -8,7 +9,7 @@ from logging.handlers import RotatingFileHandler
|
|||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
from fastapi.responses import PlainTextResponse
|
||||
from fastapi.responses import JSONResponse, PlainTextResponse
|
||||
|
||||
from openflexure_microscope_server import logging as ofm_logging
|
||||
|
||||
|
|
@ -90,18 +91,18 @@ def test_max_logs():
|
|||
# But I would log five hundred times.
|
||||
for i in range(500):
|
||||
logging.info("log %s", i)
|
||||
logs = ofm_logging.OFM_HANDLER.log_history.split("\n")
|
||||
logs = ofm_logging.OFM_HANDLER.log_history
|
||||
assert len(logs) == 250
|
||||
assert logs[0].endswith("log 250")
|
||||
assert logs[-1].endswith("log 499")
|
||||
assert logs[-1]["message"] == "log 250"
|
||||
assert logs[0]["message"] == "log 499"
|
||||
# And I would log five hundred more.
|
||||
for i in range(500):
|
||||
logging.info("log %s", 500 + i)
|
||||
# Just to be the test who logged a thousand times to check your hand-el-or!
|
||||
logs = ofm_logging.OFM_HANDLER.log_history.split("\n")
|
||||
logs = ofm_logging.OFM_HANDLER.log_history
|
||||
assert len(logs) == 250
|
||||
assert logs[0].endswith("log 750")
|
||||
assert logs[-1].endswith("log 999")
|
||||
assert logs[-1]["message"] == "log 750"
|
||||
assert logs[0]["message"] == "log 999"
|
||||
|
||||
|
||||
def test_ofm_handler_ignores_uvicorn_access():
|
||||
|
|
@ -113,17 +114,17 @@ def test_ofm_handler_ignores_uvicorn_access():
|
|||
ofm_logging.OFM_HANDLER = ofm_logging.OFMHandler()
|
||||
with tmp_logging_dir() as tmpdir:
|
||||
ofm_logging.configure_logging(tmpdir)
|
||||
logs = ofm_logging.OFM_HANDLER.log_history.split("\n")
|
||||
logs = ofm_logging.OFM_HANDLER.log_history
|
||||
starting_len = len(logs)
|
||||
logging.info("Root 1")
|
||||
uvicorn_logger = logging.getLogger("uvicorn.access")
|
||||
for i in range(500):
|
||||
uvicorn_logger.info("Uvicorn log %s", i)
|
||||
logging.info("Root 2")
|
||||
logs = ofm_logging.OFM_HANDLER.log_history.split("\n")
|
||||
logs = ofm_logging.OFM_HANDLER.log_history
|
||||
assert len(logs) == starting_len + 2
|
||||
assert logs[starting_len].endswith("Root 1")
|
||||
assert logs[-1].endswith("Root 2")
|
||||
assert logs[starting_len]["message"] == "Root 1"
|
||||
assert logs[0]["message"] == "Root 2"
|
||||
|
||||
|
||||
def test_server_responses():
|
||||
|
|
@ -140,8 +141,10 @@ def test_server_responses():
|
|||
for i in range(500):
|
||||
logging.info("log %s", i)
|
||||
log_response = ofm_logging.retrieve_log()
|
||||
assert isinstance(log_response, PlainTextResponse)
|
||||
logs = log_response.body.decode().split("\n")
|
||||
# Retrieve log should get a json that can be decoded into a list
|
||||
assert isinstance(log_response, JSONResponse)
|
||||
logs = json.loads(log_response.body.decode())
|
||||
assert isinstance(logs, list)
|
||||
assert len(logs) == 250
|
||||
file_log_response = ofm_logging.retrieve_log_from_file()
|
||||
assert isinstance(file_log_response, PlainTextResponse)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue