Winston: 打字稿错误logger.stream写入功能

创建于 2018-06-30  ·  4评论  ·  资料来源: winstonjs/winston

请告诉我们您的环境:

  • _ winston版本?_

    • [] winston@2

    • [X] winston@3

  • _ node -v输出:_ v9.8.0
  • _操作系统?_ MacOS
  • _语言?_打字稿:版本2.9.1

问题是什么?

尝试在logger.stream对象中创建写函数时遇到打字错误。

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直接添加到记录器中,请尝试以下操作:

// At the bottom of your winston config add

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

然后像这样更改您的登录Express:

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

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

由于您发布的错误,我假设您使用的是打字稿,因此我的示例是用打字稿编写的。 一个小陷阱,您可能会想像这样编写流:

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

但是,这将引发异常,因为stream函数的第二个参数不是您的记录器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);
  },
};

然后像这样更改您的登录Express:

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

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

由于您发布的错误,我假设您使用的是打字稿,因此我的示例是用打字稿编写的。 一个小陷阱,您可能会想像这样编写流:

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

但是,这将引发异常,因为stream函数的第二个参数不是您的记录器info函数所期望的。 让我知道是否有帮助。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

anks333 picture anks333  ·  3评论

xungwoo picture xungwoo  ·  3评论

Buzut picture Buzut  ·  3评论

JaehyunLee-B2LiNK picture JaehyunLee-B2LiNK  ·  3评论

exortech picture exortech  ·  3评论