@apistudio/apim-cli
Version:
CLI for API Management Products
56 lines (51 loc) • 1.61 kB
text/typescript
/**
* Copyright Super iPaaS Integration LLC, an IBM Company 2024
*/
import winston from 'winston';
import DailyRotateFile from 'winston-daily-rotate-file';
import { LoggerConfig } from './logger-config.js';
export function getLogger(moduleName: string) {
return winston.loggers.get(moduleName);
}
const createFormat = (formatType: string, moduleName: string) => {
switch (formatType) {
case 'text':
return winston.format.combine(
winston.format.timestamp(),
winston.format.simple(),
winston.format.label({ label: moduleName }),
winston.format.printf(
({ level, message, label, timestamp }) =>
`${timestamp} [${label}] ${level}: ${message}`
)
);
case 'json':
default:
return winston.format.combine(
winston.format.timestamp(),
winston.format.label({ label: moduleName }),
winston.format.json()
);
}
};
export function createLoggerInstance(moduleName: string): winston.Logger {
const format = createFormat(LoggerConfig.getLoggerFormat(), moduleName);
const consoleTransport = new winston.transports.Console();
const transportList = [];
if (process.env.STU_FILE_LOG_ENABLED === 'true') {
transportList.push(new DailyRotateFile({
filename: LoggerConfig.getFilename(),
datePattern: LoggerConfig.getDatePattern(),
zippedArchive: LoggerConfig.getZippedArchive(),
maxSize: LoggerConfig.getMaxSize(),
maxFiles: LoggerConfig.getMaxFiles(),
}));
}
if (process.env.STU_CONSOLE_LOG_ENABLED === 'true') {
transportList.push(consoleTransport);
}
return winston.loggers.add(moduleName, {
format: format,
transports: transportList,
});
}