winston
バージョン?_winston@2
winston@3
node -v
出力:_ v9.8.0logger.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
同じエラーがあります
一見すると、問題は型の定義にあるように見えます。 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
関数が期待しているものではないため、これは例外をスローします。 これが役立つかどうか教えてください。
最も参考になるコメント
stream
をロガーに直接追加しているため、これは問題です。代わりにこれを試してください。次に、次のようにエクスプレスでのログインを変更します。
あなたが投稿したエラーのため、私はあなたがtypescriptを使用していると想定しているので、私の例はtypescriptで書かれています。 1つの小さな落とし穴、あなたはこのようにあなたのストリームを書きたくなるかもしれません:
ただし、stream関数の2番目の引数は、ロガー
info
関数が期待しているものではないため、これは例外をスローします。 これが役立つかどうか教えてください。