UNPKG

@seawingai/winglog

Version:

A powerful TypeScript/JavaScript logging library built on top of Pino for structured logging with enhanced features

202 lines (201 loc) 7.38 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Logger = exports.WingLog = exports.LogType = void 0; const pino_1 = __importDefault(require("pino")); const path = __importStar(require("path")); const fs = __importStar(require("fs")); var LogType; (function (LogType) { LogType["FAILED"] = "FAILED"; LogType["WARN"] = "WARN"; LogType["DEBUG"] = "DEBUG"; LogType["SUCCESS"] = "SUCCESS"; LogType["STARTED"] = "STARTED"; LogType["FINISHED"] = "FINISHED"; LogType["INFO"] = "INFO"; })(LogType || (exports.LogType = LogType = {})); class WingLog { constructor(name) { this.name = name; // Ensure logs directory exists const logsDir = path.join(process.cwd(), 'logs'); if (!fs.existsSync(logsDir)) { fs.mkdirSync(logsDir, { recursive: true }); } const logFile = path.join(logsDir, `${name}.log`); const createConsoleTransport = () => { try { return pino_1.default.transport({ target: 'pino-pretty', options: { colorize: true, translateTime: 'SYS:standard', ignore: 'pid,hostname', messageFormat: '{msg}', }, }); } catch (error) { return pino_1.default.destination(1); // stdout } }; this.logger = (0, pino_1.default)({ level: 'debug', timestamp: () => `,"time":"${new Date().toISOString()}"`, }, pino_1.default.multistream([ { level: 'debug', stream: createConsoleTransport(), }, { level: 'debug', stream: pino_1.default.destination({ dest: logFile, sync: false, mkdir: true, }), }, ])); this.logger.debug(`Logger initialized: [${this.name}]`); } formatDuration(seconds) { const mins = Math.floor(seconds / 60); let secs = Math.round(seconds % 60); if (secs === 60) { secs = 0; } return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`; } log(message, type, icon, startTime) { let timeSpent = 0; let duration = ''; icon = icon || ''; if (startTime) { timeSpent = (Date.now() - startTime) / 1000; duration = `Duration:[${this.formatDuration(timeSpent)}]:`; } let logMessage = `[${this.name}]:${duration}${message}`; switch (type) { case LogType.FAILED: this.logger.error(logMessage); break; case LogType.WARN: this.logger.warn(logMessage); break; case LogType.DEBUG: this.logger.debug(logMessage); break; case LogType.SUCCESS: case LogType.STARTED: case LogType.FINISHED: case LogType.INFO: default: this.logger.info(logMessage); break; } return Number(timeSpent.toFixed(2)); } logWithData(message, type, rec) { this.log(`${message} ${JSON.stringify(rec)}`, type, '', 0); } started(message, startTimeOrRec) { if (startTimeOrRec === undefined || typeof startTimeOrRec === 'number') { return this.log(message, LogType.STARTED, '>', startTimeOrRec); } else { this.logWithData(message, LogType.STARTED, startTimeOrRec); } } finished(message, startTimeOrRec) { if (startTimeOrRec === undefined || typeof startTimeOrRec === 'number') { return this.log(message, LogType.FINISHED, '✓', startTimeOrRec); } else { this.logWithData(message, LogType.FINISHED, startTimeOrRec); } } success(message, startTimeOrRec) { if (startTimeOrRec === undefined || typeof startTimeOrRec === 'number') { return this.log(message, LogType.SUCCESS, '+', startTimeOrRec); } else { this.logWithData(message, LogType.SUCCESS, startTimeOrRec); } } failed(message, startTimeOrRec) { if (startTimeOrRec === undefined || typeof startTimeOrRec === 'number') { return this.log(message, LogType.FAILED, 'x', startTimeOrRec); } else { this.logWithData(message, LogType.FAILED, startTimeOrRec); } } info(message, startTimeOrRec) { if (startTimeOrRec === undefined || typeof startTimeOrRec === 'number') { return this.log(message, LogType.INFO, 'i', startTimeOrRec); } else { this.logWithData(message, LogType.INFO, startTimeOrRec); } } warn(message, startTimeOrRec) { if (startTimeOrRec === undefined || typeof startTimeOrRec === 'number') { return this.log(message, LogType.WARN, '', startTimeOrRec); } else { this.logWithData(message, LogType.WARN, startTimeOrRec); } } debug(message, startTimeOrRec) { if (startTimeOrRec === undefined || typeof startTimeOrRec === 'number') { return this.log(message, LogType.DEBUG, '', startTimeOrRec); } else { this.logWithData(message, LogType.DEBUG, startTimeOrRec); } } error(message, err) { let error = err instanceof Error ? err : new Error(String(err)); let msg = `${message}: ${error.message}`; let rec = { "error": error }; this.logWithData(msg, LogType.FAILED, rec); } } exports.WingLog = WingLog; exports.Logger = WingLog;