node.js express-winston errorLogger跳过不起作用 [英] node.js express-winston errorLogger skip doesn't work

查看:197
本文介绍了node.js express-winston errorLogger跳过不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将node.js与express-winston一起用于日志记录,

I'm using node.js with express-winston for logging, like that:

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;
        }
    })
);

请注意,我正在使用跳过功能并返回true(出于测试目的),以便跳过所有错误日志记录,如此处所示:

Notice that I'm using the skip function and returning true (for test purpose) in order to skip all error logging like written here: express-winston options

但这不起作用,有什么主意吗?

but it doesn't work, any ideas?

推荐答案

根据

用于确定是否跳过日志记录的函数,默认为返回false.在已经发送响应后称为 .

对于您的示例,您想使用ignoreRoute选项,在您的情况下,该选项将是一个返回true的函数.

For your example you want to use the ignoreRoute option which for your case would be a function that returns true.

用于确定是否跳过日志记录的函数,默认为返回false.在以后的任何中间件之前称为.

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.js express-winston errorLogger跳过不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆