温斯顿定制运输与打字稿

时间:2019-06-14 05:36:22

标签: typescript winston

我很难获取使用打字稿编译的winstom自定义记录器。

我将this js代码作为起点,并考虑了来自github的this comment

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)

我尝试了几种选择,但总是最终被打字稿编译器阻止。

2 个答案:

答案 0 :(得分:1)

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

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

然后 tsc 没有任何投诉。这类似于内置file transport导入 winston-transport 的方式。

答案 1 :(得分:1)

万一有人还需要这个。

不要忘记安装“winston-transport”,因为它不是标准 winston 库的一部分。

npm i winston-transport

import Transport from 'winston-transport';
import { createLogger } from 'winston';

class CustomTransport extends Transport {
  constructor(opts) {
    super(opts);
  }
  log(info, callback) {
 // do whatever you want with log data
    callback();
  }
};


// Using transport
const transport = new CustomTransport({});

// Create a logger and consume an instance of your transport
const logger = createLogger({
  transports: [transport]
});

// Use logger
logger.info('Here I am');