gpt4 book ai didi

node.jsexpress-winston errorLogger 跳过不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 22:15:03 24 4
gpt4 key购买 nike

我使用node.js 和express-winston 进行日志记录,如下所示:

var express = require('express');
var app = express();

var winston = require('winston');
var expressWinston = require('express-winston');

var routes = require('./routes/index');

app.use("/", routes);

app.use(
expressWinston.errorLogger({
transports: [
new winston.transports.DailyRotateFile({
name: 'file',
datePattern: '_dd-MM-yyyy.log',
colorize: true,
json: true,
filename: './logs/errors/error_log',
maxsize: 50 * 1024 * 1024,
maxFiles: 10,
zippedArchive: true
}),
new winston.transports.Console({
json: true,
colorize: true
})
],
skip: function(req, res) {
return true;
}
})
);

请注意,我正在使用skip函数并返回true(用于测试目的),以便跳过所有错误日志记录,如下所示:express-winston options

但它不起作用,有什么想法吗?

最佳答案

根据 README.md 发送响应后,将调用 skip 函数:

A function to determine if logging is skipped, defaults to returning false. Called after response has already been sent.

对于您的示例,您希望使用 ignoreRoute 选项,对于您的情况,该选项将是一个返回 true 的函数。

A function to determine if logging is skipped, defaults to returning false. Called before any later middleware.

app.use(
expressWinston.errorLogger({
transports: [
new winston.transports.DailyRotateFile({
name: 'file',
datePattern: '_dd-MM-yyyy.log',
colorize: true,
json: true,
filename: './logs/errors/error_log',
maxsize: 50 * 1024 * 1024,
maxFiles: 10,
zippedArchive: true
}),
new winston.transports.Console({
json: true,
colorize: true
})
],
v------------------------------ use `ignoreRoute` instead of `skip`
ignoreRoute: function(req, res) {
return true;
}
})
);

关于node.jsexpress-winston errorLogger 跳过不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31634652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com