Log to a file and make it visible via the API
This commit is contained in:
parent
3059683186
commit
3b2fd29fb0
2 changed files with 43 additions and 4 deletions
38
src/openflexure_microscope_server/logging.py
Normal file
38
src/openflexure_microscope_server/logging.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
import os
|
||||
|
||||
from fastapi.responses import FileResponse, PlainTextResponse
|
||||
|
||||
OFM_LOG_FOLDER = "/var/openflexure/log/"
|
||||
OFM_LOG_FILE = os.path.join(OFM_LOG_FOLDER, "openflexure-microscope-server.log")
|
||||
|
||||
def configure_logging():
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.setLevel(logging.INFO)
|
||||
try:
|
||||
if not os.path.exists(OFM_LOG_FOLDER):
|
||||
os.makedirs(OFM_LOG_FOLDER)
|
||||
handler = RotatingFileHandler(
|
||||
filename = OFM_LOG_FILE,
|
||||
mode = "a",
|
||||
maxBytes = 1000000,
|
||||
backupCount = 10,
|
||||
)
|
||||
handler.setFormatter(
|
||||
logging.Formatter(
|
||||
"[%(asctime)s] [%(levelname)s] %(message)s"
|
||||
)
|
||||
)
|
||||
root_logger.addHandler(handler)
|
||||
except PermissionError as e:
|
||||
logging.warning(f"Cannot create log file at {OFM_LOG_FILE}: {e}")
|
||||
logging.info("")
|
||||
logging.info("****************************************************")
|
||||
logging.info("OFM server root logger has been set up at INFO level")
|
||||
logging.info("****************************************************")
|
||||
|
||||
|
||||
def retrieve_log() -> PlainTextResponse:
|
||||
"""The most recent 1Mb of logs from the server"""
|
||||
return FileResponse(OFM_LOG_FILE, media_type=PlainTextResponse.media_type)
|
||||
Loading…
Add table
Add a link
Reference in a new issue