Append one JSONL record to the query log. Never raises.
(
*,
kind: str,
question: str,
corpus: str,
result: str | None = None,
nodes_returned: int | None = None,
duration_ms: float | None = None,
**extra: Any,
)
| 41 | |
| 42 | |
| 43 | def log_query( |
| 44 | *, |
| 45 | kind: str, |
| 46 | question: str, |
| 47 | corpus: str, |
| 48 | result: str | None = None, |
| 49 | nodes_returned: int | None = None, |
| 50 | duration_ms: float | None = None, |
| 51 | **extra: Any, |
| 52 | ) -> None: |
| 53 | """Append one JSONL record to the query log. Never raises.""" |
| 54 | try: |
| 55 | path = _log_path() |
| 56 | if path is None: |
| 57 | return |
| 58 | if nodes_returned is None and result is not None: |
| 59 | nodes_returned = nodes_from_result(result) |
| 60 | rec: dict[str, Any] = { |
| 61 | "ts": datetime.now(timezone.utc).isoformat(), |
| 62 | "kind": kind, |
| 63 | "question": question, |
| 64 | "corpus": corpus, |
| 65 | "nodes_returned": nodes_returned, |
| 66 | } |
| 67 | if result is not None: |
| 68 | rec["result_chars"] = len(result) |
| 69 | if duration_ms is not None: |
| 70 | rec["duration_ms"] = round(duration_ms, 3) |
| 71 | for k, v in extra.items(): |
| 72 | if v is not None: |
| 73 | rec[k] = v |
| 74 | if result is not None and _log_responses(): |
| 75 | rec["response"] = result |
| 76 | path.parent.mkdir(parents=True, exist_ok=True) |
| 77 | with path.open("a", encoding="utf-8") as fh: |
| 78 | fh.write(json.dumps(rec, ensure_ascii=False) + "\n") |
| 79 | except Exception: |
| 80 | pass |