"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserLogger = exports.TunnelLogger = exports.Logger = void 0;
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const util_1 = tslib_1.__importDefault(require("util"));
const safe_1 = tslib_1.__importDefault(require("colors/safe"));
const winston_1 = require("winston");
const lodash_1 = require("lodash");
const enum_1 = require("@/@types/enum");
const config_1 = require("@/config");
const aegis_1 = require("@/utils/aegis");
require("winston-daily-rotate-file");
class Logger {
    constructor(label = '', color, logFilename) {
        this.label = label;
        this.color = color || getRandomColor();
        this.logFilename = logFilename || '%DATE%.log';
        this.initLoggerInstance();
    }
    info(...args) {
        this.log('info', ...args);
    }
    warn(...args) {
        this.log('warn', ...args);
    }
    error(...args) {
        this.log('error', ...args);
        const errMsg = args.reduce((prev, curr) => {
            if (curr instanceof Error) {
                prev += `\n${curr.stack}`;
            }
            else if (typeof curr === 'string') {
                prev += curr;
            }
            else {
                prev += String(curr);
            }
        }, '');
        aegis_1.aegis.report(new Error(errMsg));
    }
    log(level, ...args) {
        const msg = util_1.default.format(...args);
        this.loggerInstance.log(level, msg);
    }
    initLoggerInstance() {
        const transport = new winston_1.transports.DailyRotateFile({
            filename: path_1.default.join(config_1.config.logPath, this.logFilename),
            datePattern: 'YYYY-MM-DD-HH',
            zippedArchive: false,
            maxSize: '20m',
            maxFiles: '7d',
        });
        const label = safe_1.default[this.color](this.label);
        this.loggerInstance = winston_1.createLogger({
            format: winston_1.format.combine(winston_1.format.errors({ stack: true }), winston_1.format.label({ label }), winston_1.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }), winston_1.format.colorize(), winston_1.format.printf(({ level, message, label, timestamp }) => `${timestamp} ${label} ${level} ${message}`)),
            transports: [transport, new winston_1.transports.Console()],
        });
    }
}
exports.Logger = Logger;
class TunnelLogger extends Logger {
    constructor(label = '', color, logFilename) {
        super(label, color, logFilename || '%DATE%.tunnel.log');
    }
}
exports.TunnelLogger = TunnelLogger;
class UserLogger extends Logger {
}
exports.UserLogger = UserLogger;
const winstonColors = lodash_1.uniq(Object.values(enum_1.WinstonColor).map(lodash_1.lowerFirst));
function getRandomColor() {
    return winstonColors[lodash_1.random(winstonColors.length - 1)];
}
