/**
 * Structured JSON logging — all log output is one JSON object per line on stderr.
 * Optionally also writes to a log file (if initLogFile was called).
 * Respects logLevel from config: debug < info < warn < error.
 */
import { writeLogLine } from './log-file.js';
const LEVEL_RANK = {
    debug: 0,
    info: 1,
    warn: 2,
    error: 3,
};
let minLevel = 'info';
export function setLogLevel(level) {
    minLevel = level;
}
export function getLogLevel() {
    return minLevel;
}
function emit(level, component, msg, data) {
    if (LEVEL_RANK[level] < LEVEL_RANK[minLevel])
        return;
    const entry = {
        ts: new Date().toISOString(),
        level,
        component,
        msg,
    };
    if (data && Object.keys(data).length > 0) {
        entry.data = data;
    }
    const jsonLine = JSON.stringify(entry) + '\n';
    process.stderr.write(jsonLine);
    writeLogLine(jsonLine);
}
export function log(level, component, msg, data) {
    emit(level, component, msg, data);
}
export function logDebug(component, msg, data) {
    emit('debug', component, msg, data);
}
export function logInfo(component, msg, data) {
    emit('info', component, msg, data);
}
export function logWarn(component, msg, data) {
    emit('warn', component, msg, data);
}
export function logError(component, msg, data) {
    emit('error', component, msg, data);
}
//# sourceMappingURL=logger.js.map