/**
 * Structured logger with severity levels.
 *
 * All governance modules use this instead of raw process.stderr.write.
 * Outputs structured JSON to stderr by default, with optional file output.
 *
 * @connexum/ai-governance
 */
import * as fs from 'fs';
import * as path from 'path';
import { ensureDir } from './fs.js';
const LEVEL_PRIORITY = {
    DEBUG: 0,
    INFO: 1,
    WARN: 2,
    ERROR: 3,
    CRITICAL: 4,
};
export class Logger {
    config;
    minPriority;
    constructor(config) {
        this.config = {
            level: 'INFO',
            logFile: '',
            format: 'text',
            ...config,
        };
        this.minPriority = LEVEL_PRIORITY[this.config.level];
    }
    debug(message, data) {
        this.log('DEBUG', message, data);
    }
    info(message, data) {
        this.log('INFO', message, data);
    }
    warn(message, data) {
        this.log('WARN', message, data);
    }
    error(message, data) {
        this.log('ERROR', message, data);
    }
    critical(message, data) {
        this.log('CRITICAL', message, data);
    }
    log(level, message, data) {
        if (LEVEL_PRIORITY[level] < this.minPriority)
            return;
        const entry = {
            timestamp: new Date().toISOString(),
            level,
            module: this.config.module,
            message,
            ...(data ? { data } : {}),
        };
        const output = this.config.format === 'json'
            ? JSON.stringify(entry)
            : `[${entry.timestamp}] [${level}] [${this.config.module}] ${message}${data ? ' ' + JSON.stringify(data) : ''}`;
        process.stderr.write(output + '\n');
        if (this.config.logFile) {
            try {
                ensureDir(path.dirname(this.config.logFile));
                fs.appendFileSync(this.config.logFile, JSON.stringify(entry) + '\n');
            }
            catch {
                // Logger must never throw
            }
        }
    }
}
/**
 * Create a logger for a specific module.
 */
export function createLogger(module, options) {
    return new Logger({ module, ...options });
}
//# sourceMappingURL=log.js.map