winston
版本?_winston@2
winston@3
node -v
输出:_ v9.8.0尝试在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(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
函数所期望的。 让我知道是否有帮助。
最有用的评论
这是一个问题,因为您是将
stream
直接添加到记录器中,请尝试以下操作:然后像这样更改您的登录Express:
由于您发布的错误,我假设您使用的是打字稿,因此我的示例是用打字稿编写的。 一个小陷阱,您可能会想像这样编写流:
但是,这将引发异常,因为stream函数的第二个参数不是您的记录器
info
函数所期望的。 让我知道是否有帮助。