UNPKG

@apistudio/apim-cli

Version:

CLI for API Management Products

68 lines (62 loc) 1.95 kB
/** * Copyright Super iPaaS Integration LLC, an IBM Company 2024 */ import winston from 'winston'; import {getLogger,createLoggerInstance} from './utils.js'; import {Components, ComponentCodes} from './constants/component.js'; import { MESSAGES } from './constants/log-messages.js'; export class Logger { componentCode: string; moduleName: Components; logger: winston.Logger; formatMessage(template: string, ...params: string[]): string { if (params.length === 0) { return template; } return template.replace(/{(\d+)}/g, (match, number) => { const index = Number(number); return typeof params[index] !== 'undefined' ? params[index] : match; }); } constructor(moduleName: Components) { this.moduleName = moduleName; this.logger = createLoggerInstance(moduleName.toString()); this.componentCode = ComponentCodes[moduleName]; if (!this.componentCode) { throw new Error('Invalid component'); } } logInfo(messageKey: string, ...params: string[]) { this.logger.log({ level: 'info', message: `[${this.componentCode}.${messageKey}] ${this.formatMessage(MESSAGES[messageKey], ...params)}` }); } logError(messageKey: string, ...params: string[]) { this.logger.log({ level: 'error', message: `[${this.componentCode}.${messageKey}] ${this.formatMessage(MESSAGES[messageKey], ...params)}` }); } logDebug(messageKey: string, ...params: string[]) { this.logger.log({ level: 'debug', message: `[${this.componentCode}.${messageKey}] ${this.formatMessage(MESSAGES[messageKey], ...params)}` }); } logWarn(messageKey: string, ...params: string[]) { this.logger.log({ level: 'warn', message: `[${this.componentCode}.${messageKey}] ${this.formatMessage(MESSAGES[messageKey], ...params)}` }); } setLogLevel(moduleName:string,level: string): number { if( winston.loggers.has(moduleName)) { getLogger(moduleName).level = level; return 0; } else { return -1; } } }