winston custom transport with typescript

ope*_*sas 4 winston typescript

I'm having a hard time trying to get a winstom custom logger compiled using typescript.

I'm taking this js code as starting point and taking into account this comment from 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();
  }
};
Run Code Online (Sandbox Code Playgroud)

But I get the error:

Type 'typeof TransportStream' is not a constructor function type.ts(2507)
Run Code Online (Sandbox Code Playgroud)

I tried with several alternatives but always end up blocked by typescript compiler.

dav*_*ewy 9

当我用

 import Transport = require('winston-transport');
Run Code Online (Sandbox Code Playgroud)

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


Roh*_*ati 8

万一有人还需要这个。

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

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');

Run Code Online (Sandbox Code Playgroud)