UNPKG

newmax-utils

Version:
100 lines (99 loc) 4.22 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Winston = void 0; const winston_1 = __importDefault(require("winston")); const node_1 = require("@logtail/node"); const winston_2 = require("@logtail/winston"); const env_1 = require("../configs/env"); const winston_config_1 = require("../configs/winston.config"); const TelegramTransport_1 = __importDefault(require("../utils/TelegramTransport")); const tgtopics_1 = require("../enums/tgtopics"); winston_1.default.addColors(winston_config_1.winstonConfig.colors); const { combine, timestamp, errors, json, simple, printf } = winston_1.default.format; /** * Inversion of control container for winston logger instances. */ class Winston { _env; _port; _token; _schema; _service; _thread_id; _lowLevel; _logtail; constructor(options) { this._env = env_1.env.NODE_ENV || 'development'; this._port = env_1.env.PORT; this._token = env_1.env.TELEGRAM_BOT; this._logtail = env_1.env.BETTERSTACK_TOKEN ? new node_1.Logtail(env_1.env.BETTERSTACK_TOKEN) : null; this._schema = options?.schema || null; this._service = env_1.env.SERVICE_NAME || 'common'; this._thread_id = tgtopics_1.tgTreads[this._service]; this._lowLevel = Object.entries(winston_config_1.winstonConfig.levels).sort((a, b) => b[1] - a[1])[0][0]; } createInstance = (params) => { const consoleConfig = { level: params?.console?.level || this._lowLevel, format: combine(simple(), timestamp({ format: 'HH:mm:ss' }), printf(this._formatMessage)), }; const fileConfig = { filename: `./cache/logs/${this._schema}.log`, level: params?.file?.level || this._lowLevel, format: combine(json(), errors({ stack: true })), }; const telegramConfig = { level: params?.telegram?.level, thread_id: this._thread_id, }; const transports = []; if (this._logtail) { transports.push(new winston_2.LogtailTransport(this._logtail, { level: this._lowLevel })); } if (!params?.console?.disable) { transports.push(new winston_1.default.transports.Console(consoleConfig)); } if (!params?.file?.disable && this._schema) { transports.push(new winston_1.default.transports.File(fileConfig)); } if (!params?.telegram?.disable && this._token) { transports.push(new TelegramTransport_1.default(telegramConfig, params?.telegram?.require || false)); } return winston_1.default.createLogger({ levels: winston_config_1.winstonConfig.levels, format: combine(timestamp({ format: 'YYYY-MM-DD HH:mm:ss' })), defaultMeta: { env: this._env, port: this._port, service: this._service, schema: this._schema, fn: params?.fn || null, }, transports, }); }; colorize = (color, message) => { const colorCode = winston_config_1.winstonConfig.colorsCode[color]; return `\x1b[${colorCode}m${message}\x1b[0m`; }; _formatMessage = (info) => { const { timestamp, level, schema, fn, message, stack } = info; const fastInfoColor = (winston_config_1.winstonConfig.colors[level] || winston_config_1.winstonConfig.colors.default); const schemaInfo = schema ? ` [${schema}] ` : ' '; const fastInfo = this.colorize(fastInfoColor, `${timestamp}${schemaInfo}${level}:`); const colorFnName = fn ? this.colorize('grey', fn) + ' ' : ''; let result = `${fastInfo} ${colorFnName}`; if (message) result += message; if (level === winston_config_1.winstonConfig.levelsEnums.debug) result += '\n' + JSON.stringify(info, null, 2); if (stack) result += `\n${this.colorize('grey', stack)}`; return result; }; } exports.Winston = Winston;