/**
 * Structured logging module for Vaspera Hardening MCP
 *
 * All logs are written to stderr in JSON format to avoid interfering
 * with MCP protocol communication on stdout.
 */
/**
 * Get the current log level from environment variable.
 * Defaults to "info" if not set.
 */
function getLogLevel() {
    const level = process.env.VASPERA_LOG_LEVEL?.toLowerCase();
    if (level === "debug" || level === "info" || level === "warn" || level === "error") {
        return level;
    }
    return "info";
}
const LOG_LEVELS = {
    debug: 0,
    info: 1,
    warn: 2,
    error: 3,
};
/**
 * Check if a log level should be output based on current configuration.
 */
function shouldLog(level) {
    const currentLevel = getLogLevel();
    return LOG_LEVELS[level] >= LOG_LEVELS[currentLevel];
}
/**
 * Format a log entry as a JSON string.
 */
function formatLog(level, message, context) {
    const entry = {
        timestamp: new Date().toISOString(),
        level,
        message,
    };
    if (context) {
        // Flatten context into the entry, but don't override reserved keys
        for (const [key, value] of Object.entries(context)) {
            if (key !== "timestamp" && key !== "level" && key !== "message") {
                entry[key] = value;
            }
        }
    }
    return JSON.stringify(entry);
}
/**
 * Write a log entry to stderr.
 */
function writeLog(level, message, context) {
    if (shouldLog(level)) {
        console.error(formatLog(level, message, context));
    }
}
/**
 * Structured logger instance.
 *
 * All logs are written to stderr in JSON format. The log level can be
 * controlled via the VASPERA_LOG_LEVEL environment variable.
 *
 * @example
 * ```typescript
 * import { logger } from "./logger.js";
 *
 * logger.info("certification.started", { certId: "cert-123", agents: ["security", "reliability"] });
 * logger.error("certification.failed", { certId: "cert-123", error: "Agent timeout" });
 * ```
 */
export const logger = {
    /**
     * Log a debug message. Only shown when VASPERA_LOG_LEVEL=debug.
     */
    debug: (message, context) => {
        writeLog("debug", message, context);
    },
    /**
     * Log an informational message.
     */
    info: (message, context) => {
        writeLog("info", message, context);
    },
    /**
     * Log a warning message.
     */
    warn: (message, context) => {
        writeLog("warn", message, context);
    },
    /**
     * Log an error message.
     */
    error: (message, context) => {
        writeLog("error", message, context);
    },
};
/**
 * Create a child logger with preset context fields.
 *
 * @param baseContext - Context fields to include in all log entries
 * @returns A logger instance with the preset context
 *
 * @example
 * ```typescript
 * const certLogger = createChildLogger({ certId: "cert-123" });
 * certLogger.info("agent.started", { agent: "security" });
 * // Output: {"timestamp":"...","level":"info","message":"agent.started","certId":"cert-123","agent":"security"}
 * ```
 */
export function createChildLogger(baseContext) {
    const mergeContext = (context) => ({
        ...baseContext,
        ...context,
    });
    return {
        debug: (message, context) => {
            writeLog("debug", message, mergeContext(context));
        },
        info: (message, context) => {
            writeLog("info", message, mergeContext(context));
        },
        warn: (message, context) => {
            writeLog("warn", message, mergeContext(context));
        },
        error: (message, context) => {
            writeLog("error", message, mergeContext(context));
        },
    };
}
//# sourceMappingURL=logger.js.map