【问题标题】:Extend Typescript definition for winston transport为 winston 传输扩展 Typescript 定义
【发布时间】:2018-07-30 21:21:36
【问题描述】:

我添加了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] 类型“Transports”上不存在属性“Logstash”

我的问题:

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

【问题讨论】:

  • 你把winston-extend.d.ts文件放在哪里了?你修改过 tsconfig.json 吗?

标签: typescript typescript-typings


【解决方案1】:

我解决了以下问题:

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

【讨论】:

    【解决方案2】:

    当你扩充winston Transports接口时,你需要用module "winston" { ... }包围它

    这似乎有效:

    declare module "winston-logstash" {
      import winston = require("winston");
      interface IOptions {
        port: number;
        node_name: string;
        host: string;
      }
    
      interface Static {
        new (opts: IOptions): Instance;
      }
    
      interface Instance extends winston.TransportInstance {
      }
    
      module "winston" {
        interface Transports {
          Logstash: Static;
        }
      }
    
      var Logstash: Static;
      export = Logstash;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-12
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 2019-05-20
      • 1970-01-01
      • 2018-07-18
      相关资源
      最近更新 更多