Winston: typscriptエラーlogger.stream書き込み関数

作成日 2018年06月30日  ·  4コメント  ·  ソース: winstonjs/winston

あなたの環境について教えてください:

  • _ winstonバージョン?_

    • [] winston@2

    • [X] winston@3

  • _ node -v出力:_ v9.8.0
  • _オペレーティングシステム?_MacOS
  • _Language?_ typescript:バージョン2.9.1

何が問題ですか?

logger.streamオブジェクトに書き込み関数を作成しようとすると、typescriptエラーが発生します。

error TS2322: Type '{ write(message: any, encoding: any): void; }' is not assignable to type '(options?: any) => ReadableStream'.
  Object literal may only specify known properties, and 'write' does not exist in type '(options?: any) => ReadableStream'.

39             write(message: any, encoding: any) {
               ~~~~~

代わりに何が起こると思いますか?

エラーなし

その他

const options = {
            file: {
                level: "info",
                filename: `${appRoot}/logs/app.log`,
                handleExceptions: true,
                json: true,
                maxsize: 5242880, // 5MB
                maxFiles: 5,
                colorize: false,
            },
            console: {
                level: "debug",
                handleExceptions: true,
                json: false,
                colorize: true,
            },
        };

        this.logger = winston.createLogger({
            transports: [
                new winston.transports.File(options.file),
                new winston.transports.Console(options.console),
            ],
            exitOnError: false, // do not exit on handled exceptions
        });

        // create a stream object with a 'write' function that will be used by `morgan`
        this.logger.stream = {

            write(message: any, encoding: any) {
                // use the 'info' log level so the output will be picked up by both transports (file and console)
                this.logger.info(message);
            },
        };

私はこのチュートリアルに従っています:

https://www.digitalocean.com/community/tutorials/how-to-use-winston-to-log-node-js-applications

bug

最も参考になるコメント

streamをロガーに直接追加しているため、これは問題です。代わりにこれを試してください。

// At the bottom of your winston config add

export const stream = {
  write: (message) => {
    logger.info(message);
  },
};

次に、次のようにエクスプレスでのログインを変更します。

import { stream } from 'config/winston.ts';
import * as morgan from 'morgan';

app.use(morgan('combined', { stream }));

あなたが投稿したエラーのため、私はあなたがtypescriptを使用していると想定しているので、私の例はtypescriptで書かれています。 1つの小さな落とし穴、あなたはこのようにあなたのストリームを書きたくなるかもしれません:

export const stream = {
  write: logger.info,
};

ただし、stream関数の2番目の引数は、ロガーinfo関数が期待しているものではないため、これは例外をスローします。 これが役立つかどうか教えてください。

全てのコメント4件

同じエラーがあります

一見すると、問題は型の定義にあるように見えます。 stream(options?: any): NodeJS.ReadableStream;二重である必要があると思います。

編集:実際には書き込み可能でもそれを行うことができます。 私はそれを機能させることができるかどうかを確認します。

@DABHこれを見て

streamをロガーに直接追加しているため、これは問題です。代わりにこれを試してください。

// At the bottom of your winston config add

export const stream = {
  write: (message) => {
    logger.info(message);
  },
};

次に、次のようにエクスプレスでのログインを変更します。

import { stream } from 'config/winston.ts';
import * as morgan from 'morgan';

app.use(morgan('combined', { stream }));

あなたが投稿したエラーのため、私はあなたがtypescriptを使用していると想定しているので、私の例はtypescriptで書かれています。 1つの小さな落とし穴、あなたはこのようにあなたのストリームを書きたくなるかもしれません:

export const stream = {
  write: logger.info,
};

ただし、stream関数の2番目の引数は、ロガーinfo関数が期待しているものではないため、これは例外をスローします。 これが役立つかどうか教えてください。

このページは役に立ちましたか?
0 / 5 - 0 評価