在打字稿中使用温斯顿
Using Winston in typescript
我不知道如何在打字稿中使用日志记录模块 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'.
我已将 winston
和 @types/winston
添加到我的项目中。
编辑: 完成 Joshua 的回答,似乎默认情况下 winston 登录到...无处。您必须添加传输才能使其工作:
import * as logger from "winston";
logger.configure({
level: 'debug',
transports: [
new logger.transports.Console({
colorize: true
})
]
});
以下是 Winston 的类型定义:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/winston/index.d.ts
如果您查看文件底部,默认导出声明为 const
,因此当您尝试修改 level
属性 时,它会向您抱怨,因为您正在尝试修改 const
对象。以下是相关行:
declare const winston: winston.Winston;
export = winston;
与其尝试直接设置 属性,不如尝试使用 configure
方法(请注意,您的 logger
导入类型为 Winston
:
logger.configure({
level: 'verbose',
...
})
如果这不起作用(我不确定配置调用还期望什么),您可以尝试创建一个新的 LoggerInstance
,您将能够对其进行修改。
你的第二个错误是因为你传递了一个 Error
对象,而这个对象应该是 string
。 Winston.info 的声明在这里:
info: LeveledLogMethod;
这里是 LeveledLogMethod 接口:
interface LeveledLogMethod {
(msg: string, callback: LogCallback): LoggerInstance;
(msg: string, meta: any, callback: LogCallback): LoggerInstance;
(msg: string, ...meta: any[]): LoggerInstance;
}
我不知道如何在打字稿中使用日志记录模块 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'.
我已将 winston
和 @types/winston
添加到我的项目中。
编辑: 完成 Joshua 的回答,似乎默认情况下 winston 登录到...无处。您必须添加传输才能使其工作:
import * as logger from "winston";
logger.configure({
level: 'debug',
transports: [
new logger.transports.Console({
colorize: true
})
]
});
以下是 Winston 的类型定义:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/winston/index.d.ts
如果您查看文件底部,默认导出声明为 const
,因此当您尝试修改 level
属性 时,它会向您抱怨,因为您正在尝试修改 const
对象。以下是相关行:
declare const winston: winston.Winston;
export = winston;
与其尝试直接设置 属性,不如尝试使用 configure
方法(请注意,您的 logger
导入类型为 Winston
:
logger.configure({
level: 'verbose',
...
})
如果这不起作用(我不确定配置调用还期望什么),您可以尝试创建一个新的 LoggerInstance
,您将能够对其进行修改。
你的第二个错误是因为你传递了一个 Error
对象,而这个对象应该是 string
。 Winston.info 的声明在这里:
info: LeveledLogMethod;
这里是 LeveledLogMethod 接口:
interface LeveledLogMethod {
(msg: string, callback: LogCallback): LoggerInstance;
(msg: string, meta: any, callback: LogCallback): LoggerInstance;
(msg: string, ...meta: any[]): LoggerInstance;
}