UNPKG

@jcamacaro96/utils

Version:
61 lines (60 loc) 2.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Logger = void 0; /* eslint-disable @typescript-eslint/strict-boolean-expressions */ const moment_1 = __importDefault(require("moment")); const winston_1 = __importDefault(require("winston")); const time_1 = require("../constant/time"); const format_1 = require("./format"); class Logger { constructor(method, trackId = '') { this.subMethod = ''; this.createFile = winston_1.default.createLogger({ format: winston_1.default.format.printf((info) => { const level = info.level.toUpperCase(); const logTime = moment_1.default().format('dddd, MMMM D YYYY, h:mm:ss a'); const message = info.message; const methods = this.subMethod ? `${method} -> ${this.subMethod}` : `${method}`; const lineLog = `${logTime} | ${level} | ${trackId} | ${methods} | ${message}`; return lineLog; }), transports: [ new winston_1.default.transports.Console({ format: winston_1.default.format.combine(winston_1.default.format.colorize(), winston_1.default.format.combine(winston_1.default.format.colorize({ all: true }))) }), new winston_1.default.transports.File({ filename: `./logs/${format_1.dateFormat(time_1.Time.YEAR)}-${format_1.dateFormat(time_1.Time.MONTH)}-${format_1.dateFormat(time_1.Time.DAY)}/tracking.log` }) ] }); } debug(message, payload) { this.createFile.log('debug', this.transformMsg(message, payload)); } info(message, payload = null) { this.createFile.log('info', this.transformMsg(message, payload)); } error(message) { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions this.createFile.log('error', `\n === STACK === \n ${message.stack} \n === NAME === \n ${message.name} \n === MESSAGE === \n ${message.message}`); } addSubMethod(subMethod) { this.subMethod = subMethod; } cleanSubMethod() { this.subMethod = ''; } transformMsg(message, payload) { let msg = message; if (payload) { msg += ` ${JSON.stringify(payload, null, 4)}`; } return msg; } } exports.Logger = Logger;