/** * @fileoverview Core types for log parsing and plugin system * @module parser/types */ /** * Represents a parsed log entry with normalized fields */ export interface LogEntry { /** Original raw log data (string or parsed object) */ raw: unknown; /** Extracted timestamp string */ timestamp?: string; /** Log level (error, warn, info, debug, etc.) */ level?: string; /** Log message content */ message?: string; /** All extracted fields as key-value pairs */ fields: Record; } /** * Interface for log format parsers * Implement this to add support for new log formats */ export interface LogParser { /** Unique name for this parser */ name: string; /** * Detect if a line matches this format * @param line - Raw log line to test * @returns true if this parser can handle the line */ detect(line: string): boolean; /** * Parse a log line into a LogEntry * @param line - Raw log line to parse * @returns Parsed LogEntry or null if parsing fails */ parse(line: string): LogEntry | null; } /** * Configuration for a custom log format * Used in ~/.loqrc or loq.config.ts */ export interface CustomFormatConfig { /** Unique name for this format */ name: string; /** Pattern to detect this format - can be regex or function */ detect: RegExp | ((line: string) => boolean); /** Parser function or field mapping configuration */ parse: ((line: string) => Partial | null) | FieldMapping; } /** * Field mapping configuration for regex-based parsing * Maps regex capture groups to LogEntry fields */ export interface FieldMapping { /** Regex pattern with capture groups */ pattern: RegExp; /** Map group names/indices to LogEntry fields */ fields: { /** Capture group for timestamp (name or index) */ timestamp?: string | number; /** Capture group for level (name or index) */ level?: string | number; /** Capture group for message (name or index) */ message?: string | number; /** Additional field mappings */ [key: string]: string | number | undefined; }; } /** * Configuration file structure for loq * Loaded from ~/.loqrc, ./loq.config.ts, etc. */ export interface LoqConfig { /** Custom format definitions */ formats?: CustomFormatConfig[]; /** Default output format */ defaultOutput?: 'color' | 'table' | 'json' | 'csv'; /** Default log format (override auto-detect) */ defaultFormat?: string; /** Aliases for common queries */ aliases?: Record; } /** * Create a LogParser from a CustomFormatConfig * @param config - Custom format configuration * @returns A LogParser instance * @example * ```ts * const parser = createParserFromConfig({ * name: 'my-format', * detect: /^\[.*\]/, * parse: (line) => ({ message: line, fields: {} }) * }); * ``` */ export function createParserFromConfig(config: CustomFormatConfig): LogParser { const detectFn = typeof config.detect === 'function' ? config.detect : (line: string) => (config.detect as RegExp).test(line); const parseFn = typeof config.parse === 'function' ? config.parse : createFieldMappingParser(config.parse); return { name: config.name, detect: detectFn, parse(line: string): LogEntry | null { try { const result = parseFn(line); if (!result) return null; return { raw: line, timestamp: result.timestamp, level: result.level, message: result.message, fields: result.fields || {}, }; } catch { return null; } }, }; } /** * Create a parser function from a FieldMapping configuration * @internal */ function createFieldMappingParser(mapping: FieldMapping): (line: string) => Partial | null { return (line: string) => { const match = line.match(mapping.pattern); if (!match) return null; const fields: Record = {}; const entry: Partial = { fields }; for (const [fieldName, groupRef] of Object.entries(mapping.fields)) { if (groupRef === undefined) continue; let value: string | undefined; if (typeof groupRef === 'number') { value = match[groupRef]; } else if (match.groups && groupRef in match.groups) { value = match.groups[groupRef]; } if (value !== undefined) { if (fieldName === 'timestamp' || fieldName === 'level' || fieldName === 'message') { entry[fieldName] = value; } fields[fieldName] = value; } } return entry; }; }