node.js express-winston errorLogger skip doesn'

2019-07-13 14:37发布

问题:

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