首页 > 解决方案 > 带有打字稿的winston自定义传输

问题描述

我很难尝试使用打字稿编译winstom 自定义记录器。

将此 js代码作为起点,并考虑到来自 github 的评论:

import * as Transport from 'winston-transport'

//
// Inherit from `winston-transport` so you can take advantage
// of the base functionality and `.exceptions.handle()`.
//
module.exports = class YourCustomTransport extends Transport {
  constructor(opts) {
    super(opts);
    //
    // Consume any custom options here. e.g.:
    // - Connection information for databases
    // - Authentication information for APIs (e.g. loggly, papertrail, 
    //   logentries, etc.).
    //
  }

  log(info, callback) {
    setImmediate(() => {
      this.emit('logged', info);
    });

    // Perform the writing to the remote service
    callback();
  }
};

但我得到了错误:

Type 'typeof TransportStream' is not a constructor function type.ts(2507)

我尝试了几种替代方法,但总是被打字稿编译器阻止。

标签: typescriptwinston

解决方案


当我将您的导入语句替换为

 import Transport = require('winston-transport');

那么tsc没有任何抱怨。这类似于内置文件传输导入winston-transport 的方式


推荐阅读