import { currentTraceId } from './trace-context';

/**
 * Minimal structured logger (P9 observability). One JSON line per event, always
 * carrying the current request's trace id — so the mandated QA gate ("trace id
 * appears in logs") holds and QA/devs can grep a trace end to end.
 */
export function logJson(level: 'info' | 'warn' | 'error', message: string, fields: Record<string, unknown> = {}): void {
  const line = JSON.stringify({ ts: new Date().toISOString(), level, message, traceId: currentTraceId(), ...fields });
  // eslint-disable-next-line no-console
  (level === 'error' ? console.error : level === 'warn' ? console.warn : console.log)(line);
}
