MCPcopy Create free account
hub / github.com/hunvreus/devpush / parse_structured_log

Function parse_structured_log

app/utils/log.py:46–69  ·  view source on GitHub ↗

Parse a log line, handling JSON structured logs. Returns (message, level) tuple.

(log_line: str)

Source from the content-addressed store, hash-verified

44
45
46def parse_structured_log(log_line: str) -> tuple[str, str]:
47 """Parse a log line, handling JSON structured logs.
48
49 Returns (message, level) tuple.
50 """
51 if not log_line or not log_line.strip().startswith("{"):
52 return log_line, _get_level_from_text(log_line)
53
54 try:
55 data = json.loads(log_line)
56 except (json.JSONDecodeError, ValueError):
57 return log_line, _get_level_from_text(log_line)
58
59 if not isinstance(data, dict):
60 return log_line, _get_level_from_text(log_line)
61
62 msg = data.get("msg") or data.get("message") or data.get("body") or ""
63 level_raw = data.get("level") or data.get("levelname") or data.get("severity") or ""
64
65 if not msg:
66 return log_line, _get_level_from_text(log_line)
67
68 level = LEVEL_ALIASES.get(level_raw.lower(), "INFO") if level_raw else "INFO"
69 return msg, level
70
71
72def _get_level(log_line: str) -> str:

Callers 3

_format_loki_logMethod · 0.90
get_logsMethod · 0.90
_get_levelFunction · 0.85

Calls 1

_get_level_from_textFunction · 0.85

Tested by

no test coverage detected