node.js express-winston errorLogger skip doesn't work

℡╲_俬逩灬. 提交于 2019-12-11 03:34:21

问题


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

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?


回答1:


the skip function is called after response has been sent as per the README.md:

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

For your example you want to use the ignoreRoute option which for your case would be a function that returns 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;
    }
  })
);


来源:https://stackoverflow.com/questions/31634652/node-js-express-winston-errorlogger-skip-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!