typescript 在打字稿中使用温斯顿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45171068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:43:45  来源:igfitidea点击:

Using Winston in typescript

typescriptwinston

提问by Christophe Le Besnerais

I can't figure how to use the logging module Winston in typescript. I have an error when I try to set the logger level, and another one when I try to log an error:

我不知道如何在打字稿中使用日志记录模块 Winston。当我尝试设置记录器级别时出现错误,当我尝试记录错误时出现另一个错误:

import * as logger from "winston";

logger.level = 'debug';
// [ts] Cannot assign to 'level' because it is a constant or a read-only property.

logger.error(new Error('test'));
// [ts] Argument of type 'Error' is not assignable to parameter of type 'string'.

I have added both winstonand @types/winstonto my project.

我已将winston和添加@types/winston到我的项目中。



Edit:to complete Joshua answer, it seem that by default winston logs to... nowhere. You have to add a transport to make it work:

编辑:要完成 Joshua 的回答,似乎默认情况下 winston 会登录到...无处可去。您必须添加传输才能使其工作:

import * as logger from "winston";

logger.configure({
    level: 'debug',
    transports: [
        new logger.transports.Console({
            colorize: true
        })
    ]
});

采纳答案by Joshua Breeden

Here are the type definitions for Winston:

以下是 Winston 的类型定义:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/winston/index.d.ts

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/winston/index.d.ts

If you look at the bottom of the file, the default export is declared as const, so when you try to modify the levelproperty, it complains at you because you are trying to modify a constobject. Here are the relevant lines:

如果您查看文件的底部,默认导出声明为const,因此当您尝试修改该level属性时,它会向您抱怨,因为您正在尝试修改一个const对象。以下是相关行:

declare const winston: winston.Winston;
export = winston;

Instead of trying to set that property directly, try using the configuremethod (note that your loggerimport is of type Winston:

不要尝试直接设置该属性,而是尝试使用该configure方法(请注意,您的logger导入类型为Winston

logger.configure({
    level: 'verbose',
    ...
})

If this doesn't work (I'm not sure exactly what else the configure call expects), you might try creating a new LoggerInstance, which you will be able to modify.

如果这不起作用(我不确定 configure 调用还期望什么),您可以尝试创建一个 new LoggerInstance,您将能够对其进行修改。

Your second error is because you're passing an Errorobject where a stringis expected. The declaration of Winston.info is here:

你的第二个错误是因为你传递了Error一个string预期a的对象。Winston.info 的声明在这里:

info: LeveledLogMethod;

And here is the LeveledLogMethod interface:

这是 LeveledLogMethod 接口:

interface LeveledLogMethod {
    (msg: string, callback: LogCallback): LoggerInstance;
    (msg: string, meta: any, callback: LogCallback): LoggerInstance;
    (msg: string, ...meta: any[]): LoggerInstance;
}