mirror of
https://github.com/langgenius/dify.git
synced 2026-04-15 18:06:36 +08:00
refactor(api): type _build_log_dict return with LogDict TypedDict (#34983)
Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
parent
534fea7104
commit
d06bc2f2e1
@ -3,7 +3,7 @@
|
|||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from typing import Any, TypedDict
|
from typing import Any, NotRequired, TypedDict
|
||||||
|
|
||||||
import orjson
|
import orjson
|
||||||
|
|
||||||
@ -16,6 +16,19 @@ class IdentityDict(TypedDict, total=False):
|
|||||||
user_type: str
|
user_type: str
|
||||||
|
|
||||||
|
|
||||||
|
class LogDict(TypedDict):
|
||||||
|
ts: str
|
||||||
|
severity: str
|
||||||
|
service: str
|
||||||
|
caller: str
|
||||||
|
message: str
|
||||||
|
trace_id: NotRequired[str]
|
||||||
|
span_id: NotRequired[str]
|
||||||
|
identity: NotRequired[IdentityDict]
|
||||||
|
attributes: NotRequired[dict[str, Any]]
|
||||||
|
stack_trace: NotRequired[str]
|
||||||
|
|
||||||
|
|
||||||
class StructuredJSONFormatter(logging.Formatter):
|
class StructuredJSONFormatter(logging.Formatter):
|
||||||
"""
|
"""
|
||||||
JSON log formatter following the specified schema:
|
JSON log formatter following the specified schema:
|
||||||
@ -55,9 +68,9 @@ class StructuredJSONFormatter(logging.Formatter):
|
|||||||
|
|
||||||
return json.dumps(log_dict, default=str, ensure_ascii=False)
|
return json.dumps(log_dict, default=str, ensure_ascii=False)
|
||||||
|
|
||||||
def _build_log_dict(self, record: logging.LogRecord) -> dict[str, Any]:
|
def _build_log_dict(self, record: logging.LogRecord) -> LogDict:
|
||||||
# Core fields
|
# Core fields
|
||||||
log_dict: dict[str, Any] = {
|
log_dict: LogDict = {
|
||||||
"ts": datetime.now(UTC).isoformat(timespec="milliseconds").replace("+00:00", "Z"),
|
"ts": datetime.now(UTC).isoformat(timespec="milliseconds").replace("+00:00", "Z"),
|
||||||
"severity": self.SEVERITY_MAP.get(record.levelno, "INFO"),
|
"severity": self.SEVERITY_MAP.get(record.levelno, "INFO"),
|
||||||
"service": self._service_name,
|
"service": self._service_name,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user