function encode(value) {
    if (value === undefined)
        return '';
    if (typeof value === 'string')
        return JSON.stringify(value);
    if (typeof value === 'number' || typeof value === 'boolean' || value === null)
        return String(value);
    try {
        return JSON.stringify(value);
    }
    catch {
        return JSON.stringify(String(value));
    }
}
function write(level, event, fields = {}) {
    const extras = Object.entries(fields)
        .filter(([, value]) => value !== undefined)
        .map(([key, value]) => `${key}=${encode(value)}`)
        .join(' ');
    const line = `[fatmux ${new Date().toISOString()}] ${level} ${event}${extras ? ` ${extras}` : ''}`;
    if (level === 'ERROR') {
        console.error(line);
        return;
    }
    if (level === 'WARN') {
        console.warn(line);
        return;
    }
    console.log(line);
}
export const logger = {
    info(event, fields) {
        write('INFO', event, fields);
    },
    warn(event, fields) {
        write('WARN', event, fields);
    },
    error(event, fields) {
        write('ERROR', event, fields);
    }
};
export function socketDetails(socket) {
    return {
        remoteAddress: socket.remoteAddress,
        remotePort: socket.remotePort,
        localAddress: socket.localAddress,
        localPort: socket.localPort
    };
}
export function requestDetails(req) {
    return {
        method: req.method,
        url: req.url,
        httpVersion: req.httpVersion,
        ...socketDetails(req.socket)
    };
}
export function errorDetails(error) {
    if (error instanceof Error) {
        return {
            errorName: error.name,
            errorMessage: error.message,
            errorStack: error.stack
        };
    }
    return { errorMessage: String(error) };
}
//# sourceMappingURL=logging.js.map