// Bug: timestamp checked by shape only (no calendar check) and duplicate keys keep the leftmost value.export type Level = "DEBUG" | "INFO" | "WARN" | "ERROR";export type LogLine = { timestamp: string; level: Level; module?: string; message: string; fields: Record<string, string>;};export type LogParseFailure = { kind: "empty_line" } | { kind: "bad_timestamp" } | { kind: "bad_level"; level: string };export class LogParseError extends Error { declare readonly kind: LogParseFailure["kind"]; declare readonly level?: string; constructor(failure: LogParseFailure) { super(failure.kind); Object.assign(this, failure); }}export function parseLogLine(line: string): LogLine { const tokens = line.trim().split(/ +/); if (!tokens[0]) throw new LogParseError({ kind: "empty_line" }); const [timestamp, level = "", ...rest] = tokens; if (!/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ$/.test(timestamp)) throw new LogParseError({ kind: "bad_timestamp" }); if (!["DEBUG", "INFO", "WARN", "ERROR"].includes(level)) throw new LogParseError({ kind: "bad_level", level }); let module: string | undefined; if (rest[0] && /^\[.+\]$/.test(rest[0])) module = rest.shift()!.slice(1, -1); const fields: Record<string, string> = {}; while (rest.length && /^[a-z_][a-z0-9_]*=.+$/.test(rest[rest.length - 1])) { const [k, ...v] = rest.pop()!.split("="); fields[k] = v.join("="); } return { timestamp, level: level as Level, module, message: rest.join(" "), fields };}