"use strict";
// SPDX-License-Identifier: Apache-2.0
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoggerOptionLevels = exports.LogLevel = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
const winston_1 = require("winston");
const fast_safe_stringify_1 = __importDefault(require("fast-safe-stringify"));
const BaseError_1 = __importDefault(require("../../../core/error/BaseError"));
const { Console } = winston_1.transports;
const { printf } = winston_1.format;
var LogLevel;
(function (LogLevel) {
    LogLevel["TRACE"] = "TRACE";
    LogLevel["INFO"] = "INFO";
    LogLevel["ERROR"] = "ERROR";
})(LogLevel || (exports.LogLevel = LogLevel = {}));
exports.LoggerOptionLevels = {
    [LogLevel.TRACE]: 3,
    [LogLevel.INFO]: 2,
    [LogLevel.ERROR]: 0,
};
class LogService {
    static instance = new LogService();
    static defaultFormat = printf(({ level, message, timestamp, other }) => {
        const formatOther = (val) => {
            return val
                .map((e) => {
                switch (typeof e) {
                    case "object":
                        return (0, fast_safe_stringify_1.default)(e);
                    default:
                        return e;
                }
            })
                .join("\t");
        };
        return `${timestamp} - [${level}]\t${message}\t${formatOther(other)}`;
    });
    logger;
    coreConfig = {
        levels: exports.LoggerOptionLevels,
        exitOnError: false,
    };
    defaultConfig = {
        transports: new Console(),
        level: LogLevel.ERROR,
        format: LogService.defaultFormat,
    };
    constructor(opts) {
        LogService.instance = this;
        this.logger = (0, winston_1.createLogger)({
            ...(opts ? { ...this.defaultConfig, ...opts } : this.defaultConfig),
            ...this.coreConfig,
        });
    }
    static log(level, message, params) {
        this.instance.logger.log(level, message, {
            timestamp: new Date().toISOString(),
            other: params,
        });
    }
    static logError(error, ...params) {
        if (error instanceof BaseError_1.default) {
            this.log(LogLevel.ERROR, error
                .toString(), params);
        }
        else {
            this.log(LogLevel.ERROR, error, params);
        }
    }
    static logInfo(message, ...params) {
        this.log(LogLevel.INFO, message, params);
    }
    static logTrace(message, ...params) {
        this.log(LogLevel.TRACE, message, params);
    }
}
exports.default = LogService;
