Winston Logger 中的冗余

huangapple go评论63阅读模式
英文:

Redundancy in Winston Logger

问题

除了文件名之外,下面的代码存在冗余。由于我对Node.js很陌生,请建议我如何在调用应用程序的日志记录器时将文件名作为参数传递。

英文:

As there is redundancy in the below code except the file name... Please suggest me ways to reduce it as I am very new to node js. How can I pass file name as an argument while calling the logger from my application.

const loggerForDWTApps = createLogger({
    level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
    format: format.combine(
        format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss'
        }),
        format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
    transports: [new transports.File({ filename: path.join(logDir, 'XYZ.log') })
    ]    
});

const loggerForDWTService = createLogger({
    level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
    format: format.combine(
        format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss'
        }),
        format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
    transports: [new transports.File({ filename: path.join(logDir, 'ABC.log') })
    ]
});

答案1

得分: 0

function getWinstonConfig(fileName) {
   return {
      level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
      format: format.combine(
        format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss'
        }),
        format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
        transports: [new transports.File({ filename: path.join(logDir, '${fileName}.log')})]    
   }
}
const loggerForDWTApps = createLogger(getWinstonConfig('ABC'));

const loggerForDWTService = createLogger(getWinstonConfig('XYZ'));
英文:

Write a function and call it as shown below :

function getWinstonConfig(fileName) {
   return {
      level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
      format: format.combine(
        format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss'
        }),
        format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
        transports: [new transports.File({ filename: path.join(logDir, '${fileName}.log')})]    
   }
}
const loggerForDWTApps = createLogger(getWinstonConfig('ABC'));

const loggerForDWTService = createLogger(getWinstonConfig('XYZ'));

答案2

得分: 0

Here is the translated content without the code:

我建议创建一个函数,比如说createLoggerWrapper,该函数接受用于配置日志记录器的各种参数。

例如:

function createLoggerWrapper(logFile) {
  return createLogger({
    level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
    format: format.combine(
        format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss'
        }),
        format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
    transports: [new transports.File({ filename: path.join(logDir, logFile) })]
  });
}

const loggerForDWTApps = createLoggerWrapper('XYZ.log');
const loggerForDWTService = createLoggerWrapper('ABC.log');

I hope this helps!

英文:

I'd suggest creating a function, say createLoggerWrapper that accepts any varying parameters for your logger configuration.

For example:

function createLoggerWrapper(logFile) {
  return createLogger({
    level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
    format: format.combine(
        format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss'
        }),
        format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
    transports: [new transports.File({ filename: path.join(logDir, logFile) })]
  });
}

const loggerForDWTApps = createLoggerWrapper('XYZ.log');
const loggerForDWTService = createLoggerWrapper('ABC.log');

huangapple
  • 本文由 发表于 2020年1月6日 16:58:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定