Add invocation ID to log message if available
This commit is contained in:
parent
eac055d352
commit
e42aaf2547
1 changed files with 21 additions and 3 deletions
|
|
@ -19,6 +19,8 @@ from typing import Optional
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from fastapi.responses import PlainTextResponse
|
from fastapi.responses import PlainTextResponse
|
||||||
|
|
||||||
|
from labthings_fastapi.logs import inject_invocation_id
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
OFM_LOG_FILE: Optional[str] = None
|
OFM_LOG_FILE: Optional[str] = None
|
||||||
|
|
||||||
|
|
@ -50,9 +52,10 @@ def configure_logging(log_folder: str, debug: bool = False) -> None:
|
||||||
|
|
||||||
# Add OFM_HANDLER first so it can capture the error log if the
|
# Add OFM_HANDLER first so it can capture the error log if the
|
||||||
# log file can't be accessed.
|
# log file can't be accessed.
|
||||||
ofm_format_str = "[%(asctime)s] [%(levelname)s] %(message)s"
|
ofm_format_str = "[%(asctime)s] [%(levelname)s]%(invocation_id_str)s %(message)s"
|
||||||
OFM_HANDLER.setFormatter(logging.Formatter(ofm_format_str))
|
OFM_HANDLER.setFormatter(OFMConsoleFormatter(ofm_format_str))
|
||||||
OFM_HANDLER.addFilter(UvicornAccessFilter())
|
OFM_HANDLER.addFilter(UvicornAccessFilter())
|
||||||
|
OFM_HANDLER.addFilter(inject_invocation_id)
|
||||||
OFM_HANDLER.level = root_logger.level
|
OFM_HANDLER.level = root_logger.level
|
||||||
root_logger.addHandler(OFM_HANDLER)
|
root_logger.addHandler(OFM_HANDLER)
|
||||||
|
|
||||||
|
|
@ -65,9 +68,12 @@ def configure_logging(log_folder: str, debug: bool = False) -> None:
|
||||||
maxBytes=1000000,
|
maxBytes=1000000,
|
||||||
backupCount=10,
|
backupCount=10,
|
||||||
)
|
)
|
||||||
format_str = "[%(asctime)s] [%(levelname)s] <%(name)s> %(message)s"
|
format_str = (
|
||||||
|
"[%(asctime)s] [%(levelname)s] <%(name)s>%(invocation_id_str)s %(message)s"
|
||||||
|
)
|
||||||
handler.setFormatter(OFMLogFileFormatter(format_str))
|
handler.setFormatter(OFMLogFileFormatter(format_str))
|
||||||
handler.addFilter(UvicornAccessFilter())
|
handler.addFilter(UvicornAccessFilter())
|
||||||
|
handler.addFilter(inject_invocation_id)
|
||||||
root_logger.addHandler(handler)
|
root_logger.addHandler(handler)
|
||||||
|
|
||||||
except PermissionError as e:
|
except PermissionError as e:
|
||||||
|
|
@ -182,4 +188,16 @@ class UvicornAccessFilter(logging.Filter):
|
||||||
return not record.name.startswith("uvicorn.access")
|
return not record.name.startswith("uvicorn.access")
|
||||||
|
|
||||||
|
|
||||||
|
class OFMConsoleFormatter(logging.Formatter):
|
||||||
|
"""Formatter for OFM_HANDLER (the UI-facing log)."""
|
||||||
|
|
||||||
|
def format(self, record: logging.LogRecord) -> str:
|
||||||
|
"""Render the invocation ID into the record, if present."""
|
||||||
|
invocation_id = getattr(record, "invocation_id", None)
|
||||||
|
record.invocation_id_str = (
|
||||||
|
f" [invocation:{invocation_id}]" if invocation_id else ""
|
||||||
|
)
|
||||||
|
return super().format(record)
|
||||||
|
|
||||||
|
|
||||||
OFM_HANDLER = OFMHandler()
|
OFM_HANDLER = OFMHandler()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue