扩展winston传输的Typescript定义

时间:2018-02-20 13:34:25

标签: typescript typescript-typings

我添加了winston-logstash包,这是一个用于logstash的winston传输。不幸的是,它没有打字稿定义,我正在努力添加自己的打字。

我尝试了什么:

我的winston.ts:

import { Logger, LoggerInstance, transports } from 'winston';
require('winston-logstash');

export const logger: LoggerInstance = new Logger({
  transports: [
    // Console Logger Settings
    new transports.Console({
      timestamp: tsFormat,
      colorize: true,
      silent: false,
      prettyPrint: true,
      level: 'debug'
    }),

    new transports.Logstash({
      port: 28777,
      node_name: 'my node name',
      host: '127.0.0.1
    })
],
  exitOnError: false,
  colors: {
    trace: 'white',
    debug: 'green',
    info: 'blue',
    warn: 'yellow',
    crit: 'red',
    fatal: 'red'
  }
});

我的类型/ winston-extend.d.ts:

declare module "winston-logstash" {
  import winston = require("winston");
  import { TransportInstance } from 'winston';


  interface IOptions {
    port: number;
    node_name: string;
    host: string;
  }

  interface Static {
    new (opts: IOptions): Instance;
  }

  interface Instance extends winston.TransportInstance {
    log(level: string, msg: string, meta: any, cb: Function);
  }

  interface Transports {
    Logstash: Instance;
  }

  var Logstash: Static;
  export = Logstash;
}

错误:

  

[ts] Property' Logstash'类型'运输'

上不存在

我的问题:

如何为winston传输正确添加打字稿定义(特别是winston-logstash)?

2 个答案:

答案 0 :(得分:1)

当你扩充winston sub界面时,你需要用Transports

包围它

这似乎有效:

module "winston" { ... }

答案 1 :(得分:0)

我解决了以下问题:

const { Logstash } = require('winston-logstash')
...
logger.add(Logstash, {
                       port: LOGSTASH_PORT,
                       host: LOGSTASH_HOST,
                       node_name: APP_NAME
                     })