import { redactUnknown } from "./redaction.js";
export function installConsoleLogger(format) {
    const targetLog = console.log.bind(console);
    const targetWarn = console.warn.bind(console);
    const targetError = console.error.bind(console);
    if (format !== "json") {
        console.log = (...args) => {
            targetLog(toTextRecord("info", args));
        };
        console.warn = (...args) => {
            targetWarn(toTextRecord("warn", args));
        };
        console.error = (...args) => {
            targetError(toTextRecord("error", args));
        };
        return;
    }
    console.log = (...args) => {
        targetLog(JSON.stringify(toLogRecord("info", args)));
    };
    console.warn = (...args) => {
        targetLog(JSON.stringify(toLogRecord("warn", args)));
    };
    console.error = (...args) => {
        targetLog(JSON.stringify(toLogRecord("error", args)));
    };
}
function toTextRecord(level, args) {
    return `[${formatLocalTimestamp(new Date())}] ${level.toUpperCase()} ${args.map(formatArg).join(" ")}`;
}
function toLogRecord(level, args) {
    return {
        ts: new Date().toISOString(),
        level,
        event: "console",
        message: args.map(formatArg).join(" "),
    };
}
function formatArg(value) {
    return redactUnknown(value);
}
function formatLocalTimestamp(date) {
    return [
        `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`,
        `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`,
    ].join(" ");
}
function pad(value) {
    return String(value).padStart(2, "0");
}
