"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Level = void 0;
const util_1 = require("util");
exports.Level = {
    DEBUG: 1,
    VERBOSE: 2,
    LOG: 3,
    WARN: 4,
    ERROR: 5,
    FATAL: 6,
};
function getLevel(LOG_LEVEL = '') {
    return exports.Level[LOG_LEVEL.toUpperCase()] || exports.Level.LOG;
}
class Logger {
    constructor() {
        this.level = getLevel(process.env.LOG_LEVEL);
    }
    setLevel(level) {
        var _a, _b;
        this.level = level;
        process.env.LOG_LEVEL = (_b = (_a = Object.entries(exports.Level)
            .find(([k, v]) => v === level)) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.toUpperCase();
    }
    log(msg, ...optionalParams) {
        if (this.level > exports.Level.LOG) {
            return;
        }
        const logFn = Logger._getLogFn(exports.Level.LOG);
        logFn(...arguments);
    }
    verbose(msg, ...optionalParams) {
        if (this.level > exports.Level.VERBOSE) {
            return;
        }
        const logFn = Logger._getLogFn(exports.Level.VERBOSE);
        logFn(...arguments);
    }
    warn(msg, ...optionalParams) {
        if (this.level > exports.Level.WARN) {
            return;
        }
        const logFn = Logger._getLogFn(exports.Level.WARN);
        logFn(...arguments);
    }
    error(msg, ...optionalParams) {
        if (this.level > exports.Level.ERROR) {
            return;
        }
        const logFn = Logger._getLogFn(exports.Level.ERROR);
        logFn(...arguments);
    }
    fatal(msg, ...optionalParams) {
        if (this.level > exports.Level.FATAL) {
            return;
        }
        const logFn = Logger._getLogFn(exports.Level.FATAL);
        logFn(...arguments);
    }
    static _getLogFn(level) {
        const header = `[${new Date().toISOString()}]:`;
        switch (level) {
            default:
            case exports.Level.VERBOSE:
            case exports.Level.LOG:
                return (message, ...optionalParams) => {
                    process.stdout.write(header +
                        ' ' +
                        (typeof message !== 'string' ? util_1.inspect(message) : message) +
                        ' ' +
                        optionalParams.map((p) => (typeof p !== 'string' ? util_1.inspect(p) : p)).join(' ') +
                        '\n');
                };
            case exports.Level.WARN:
                return function (message, ...optionalParams) {
                    console.warn(header, message, ...optionalParams);
                };
            case exports.Level.FATAL:
                return function (message, ...optionalParams) {
                    console.error(header, message, ...optionalParams);
                    process.exit(1);
                };
            case exports.Level.ERROR:
                return function (message, ...optionalParams) {
                    console.error(header, message, ...optionalParams);
                };
        }
    }
}
exports.default = new Logger();
//# sourceMappingURL=logger.js.map