DEV Community

Discussion on: Track user behavior & resource demand with ‘mongoose-morgan’

Collapse
 
nempet profile image
Nemanja Petrovic

Hi @tharindurewatha ,

you can achieve this by specifying skip function:

// EXAMPLE: only log error responses
morgan('combined', {
  skip: function (req, res) { return res.statusCode < 400 }
})
Enter fullscreen mode Exit fullscreen mode

and in this function you just check:

req.method
Enter fullscreen mode Exit fullscreen mode

property to satisfy your condition.

Collapse
 
diegotech profile image
Diego Gallovich • Edited

@nempet thanks for replying to this comment. I am barely getting back to articles.

I also found a similar example with a bit more specificity from your own docs:

app.use(morgan({
    collection: 'error_logger',
    connectionString: 'mongodb://localhost:27017/logs-db',
    user: 'admin',
    pass: 'test12345'
   },
   {
    skip: function (req, res) {
        return res.statusCode < 400;
    }
   },
   'dev'
));
Enter fullscreen mode Exit fullscreen mode