UNPKG

yogeshs-utilities

Version:
108 lines 5.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.morganMiddleware = exports.WinstonLogger = void 0; const morgan = require("morgan"); const winston_1 = require("winston"); const path_1 = require("path"); const DailyRotateFile = require("winston-daily-rotate-file"); const path_2 = require("path"); const fs_1 = require("fs"); const app_root_path_1 = require("app-root-path"); const dotenv_1 = __importDefault(require("dotenv")); const Redactyl = require("redactyl.js"); dotenv_1.default.config(); const { combine, timestamp, json } = winston_1.format; console.log(app_root_path_1.path); const setLogsDirectory = function () { const logPath = (0, path_2.resolve)((0, path_2.join)(app_root_path_1.path, "logs")); if (!(0, fs_1.existsSync)(logPath)) { (0, fs_1.mkdirSync)(logPath); } return logPath; }; const logPath = setLogsDirectory(); const properties = ["password", "cardNumber", "email"]; var redactyl = new Redactyl({ properties, text: '[REDACTED]' }); const customFormat = winston_1.format.printf(({ timestamp, level, message, ...msgData }) => { const logEntry = { meta: { timestamp: timestamp, level: level, fileName: msgData.fileName }, data: { message, code: msgData.code, name: msgData.name, value: msgData.value, date: msgData.date, string: msgData.string, extras: { ...msgData.extras } } }; const redacted = redactyl.redact(logEntry); return JSON.stringify(redacted); }); const warnFilter = (0, winston_1.format)((info) => { return info.level === "warn" ? info : false; }); const infoFilter = (0, winston_1.format)((info) => { return info.level === "info" ? info : false; }); const errorFilter = (0, winston_1.format)((info) => { return info.level === "error" ? info : false; }); const debugFilter = (0, winston_1.format)((info) => { return info.level === "debug" ? info : false; }); class WinstonLogger { constructor(_filePath) { this.fileName = ''; this.fileName = (0, path_1.basename)(_filePath); this.logger = (0, winston_1.createLogger)({ level: process.env.LOG_LEVEL || 'info', format: winston_1.format.combine(winston_1.format.timestamp({ format: "YYYY-MM-DD hh:mm:ss A" }), customFormat), transports: [ new winston_1.transports.Console({ format: winston_1.format.combine(winston_1.format.timestamp(), customFormat) }), new DailyRotateFile({ level: 'error', filename: `${logPath}/error-%DATE%.log`, datePattern: 'YYYY-MM-DD', maxFiles: '15d', format: winston_1.format.combine(winston_1.format.timestamp(), errorFilter(), customFormat) }), new DailyRotateFile({ level: 'info', filename: `${logPath}/info-%DATE%.log`, datePattern: 'YYYY-MM-DD', maxFiles: '15d', format: winston_1.format.combine(winston_1.format.timestamp(), infoFilter(), customFormat) }), new DailyRotateFile({ level: 'debug', filename: `${logPath}/debug-%DATE%.log`, datePattern: 'YYYY-MM-DD', maxFiles: '15d', format: winston_1.format.combine(winston_1.format.timestamp(), debugFilter(), customFormat) }), new DailyRotateFile({ level: 'warn', filename: `${logPath}/warnings-%DATE%.log`, datePattern: 'YYYY-MM-DD', maxFiles: '15d', format: winston_1.format.combine(winston_1.format.timestamp(), warnFilter(), customFormat) }) ], exceptionHandlers: [new winston_1.transports.File({ filename: `${logPath}/exception.log` }),], rejectionHandlers: [new winston_1.transports.File({ filename: `${logPath}/rejections.log` }),], exitOnError: false }); } info(message, data) { this.logger.info(message, { fileName: this.fileName, ...data }); } error(error, data) { this.logger.error(error.message, { fileName: this.fileName, ...data, name: error.name, stack: error.stack }); } warn(message, data) { this.logger.warn(message, { fileName: this.fileName, ...data }); } debug(message, data) { this.logger.debug(message, { fileName: this.fileName, ...data }); } silly(message, data) { this.logger.silly(message, { fileName: this.fileName, ...data }); } } exports.WinstonLogger = WinstonLogger; const httpLogger = (0, winston_1.createLogger)({ level: 'http', format: combine(timestamp({ format: 'YYYY-MM-DD hh:mm A', }), json()), transports: [new winston_1.transports.Console()], }); const morganMiddleware = morgan(':method :url :status :res[content-length] - :response-time ms', { stream: { write: (message) => httpLogger.http(message.trim()) } }); exports.morganMiddleware = morganMiddleware; //# sourceMappingURL=winston-logger.js.map