【问题标题】:Monkey patch the node js winston loggingMonkey 修补节点 js winston 日志记录
【发布时间】:2020-07-14 00:51:56
【问题描述】:

您是否知道如何使用 shimmer(https://www.npmjs.com/package/shimmer) 对节点 js winston 日志记录方法(如 info、调试)进行猴子修补?

例如, 这是我的 winston 3.x 设置:

let winston = require('winston')
   winstonInit = winston.createLogger({
      format: winston.format.json(),
      transports: [
        new winston.transports.Console({ level: 'info' }),
        new winston.transports.File({
          name: 'winston-logging',
          level: 'info',
          filename: './log/winston.log',
          handleExceptions: true
        })
      ],
      exceptionHandlers: [
        new winston.transports.File({ filename: './log/exceptions.log', handleExceptions: true})
      ]
    });
    winston.add(winstonInit);

所以,在我的应用程序中,我可以调用它 winston.info('send this message to Slack') // output: send this message to Slack .除了显示消息,我们还执行了其他功能。

我只想对这个 winston.info() 进行修补,例如添加一些额外的功能,例如在执行 winston.info 时发送 Slack 消息通知。谢谢。

【问题讨论】:

  • 你目前是如何配置winston的?
  • @DhruvShah 我已在摘要中添加了我的 winston 设置。
  • 你可以在winston中使用http传输来发送任何webpoint。

标签: node.js winston shimmer


【解决方案1】:

有两种方法可以实现:

A. shimmer方式:

var shimmer = require('shimmer');

// Assuming that an instance of winston is accesible.
shimmer.wrap(winston, 'info-interceptor', (winstonCall) => {
    return function(arguments) {
        // arguments are the log messages sent to the winston method for logging.
        console.log(arguments)
        
        // check if executed winston function is winston.info or not.
        if(winstonCall.name === 'info'){
           // add logic to send arguments to a slack message
        }
        
        // it is important to return the original call.
        retirm winstonCall;
    }
});

注意:不建议使用function.name 方法,因此我建议不要使用微光方式。

B. non-shimmer方式

您可以更新您的 winston 配置以在 info 方法以下列方式执行时执行附加功能:

let winston = require('winston')
   winstonInit = winston.createLogger({
      format: winston.format.json(),
      transports: [
        new winston.transports.Console({ level: 'info' }),
        new winston.transports.File({
          name: 'winston-logging',
          level: 'info',
          filename: './log/winston.log',
          handleExceptions: true
        }),
        new HttpStreamTransport({
            url: 'https://yourdomain.com/log'
        })
      ],
      exceptionHandlers: [
        new winston.transports.File({ filename: './log/exceptions.log', handleExceptions: true})
      ]
    });
    
    // winston.add(winstonInit);

    const logger = {
       _sendMessageToSlack: (msg) => {
           // logic for sending message to slack
        
           return message;
       },
       info: (msg) => winstonInit.info(logger._sendMessageToSlack(msg)),
       warn: (msg) => winstonInit.warn(msg),
       error: (msg) => winstonInit.error(msg),
   };

module.exports = logger;

这将允许您在各种文件中导入logger 实例并将其用作logger.info()logger.warn()logger.error(),并在调用logger.info 时执行附加功能。

【讨论】:

  • 或者你可以直接使用winston中的slack transport。参考:github.com/winstonjs/winston/blob/master/docs/…
  • 我已经尝试在本地使用您在解决方案 A 中的代码,但很抱歉它不起作用。它没有修补 info 方法。你的非闪光方式看起来很有趣。谢谢:)
猜你喜欢
  • 2019-09-29
  • 2015-04-15
  • 2018-06-21
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
相关资源
最近更新 更多