SPB Git

Repository Public

log.ts 887 B 13 lines typescript

1/** Structured JSON log lines on stdout (PM2 collects and rotates them). Never pass secrets or document bodies. */2type Level = "debug" | "info" | "warn" | "error";3const ORDER: Record<Level, number> = { debug: 10, info: 20, warn: 30, error: 40 };4const threshold = () => ORDER[(process.env.LOG_LEVEL as Level) ?? "info"] ?? 20;5const SECRET = /(sk-ant-[A-Za-z0-9_-]{8})[A-Za-z0-9_-]+/g;67export function log(level: Level, msg: string, fields: Record<string, unknown> = {}): void {8  if (ORDER[level] < threshold()) return;9  const line = JSON.stringify({ t: new Date().toISOString(), level, msg, ...fields }).replace(SECRET, "$1…");10  (level === "error" || level === "warn" ? console.error : console.log)(line);11}12export const errInfo = (e: unknown) => (e instanceof Error ? { error: e.name, detail: e.message.slice(0, 500) } : { error: "unknown", detail: String(e).slice(0, 500) });13
Files main
Menu

Theme