Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging with winston-mongodb and express-winston

I'm trying to log request/response into MongoDB within NodeJS project using express-winston and winston-mongodb. Here is an example code that I worked so far;

const expressWinston = require('express-winston');
const winston = require('winston'); 
require('winston-mongodb').MongoDB;

const logger = expressWinston.logger({
 transports: [
    winston.add(winston.transports.MongoDB, {
        db : 'something',
        collection : 'something',
        level : 'info',
        capped : true
    })
 ]
});

I'm exporting this logger and using it my index.js;

app.use(logger);

And at the end, I'm facing 2 problems;

  1. A new entry is created in my Mongo collection for each request/response but they are empty as shown below enter image description here

  2. I got an exception even the entry is created;

    TypeError: cb is not a function at logDb.collection.insertOne.then.catch.err (\node_modules\winston-mongodb\lib\winston-mongodb.js:213:7)

Here is the code block from winston-mongodb.js that causes the exception;

this.logDb.collection(this.collection).insertOne(entry).then(()=>{
  console.error('55dddddrrr', {});
  this.emit('logged');
  **cb(null, true);**
})

I've been trying to solve this but couldn't came up with anything useful yet. Would appreciate any help on the issue.

like image 272
josh Avatar asked Jan 11 '18 08:01

josh


People also ask

What is Winston logging?

Winston allows you to implement multiple logging transports, i.e., a log can be recorded to a file, console, or database. The Logger configuration below logs to a console and a file. We'll add a transport array to the log configuration object.

Is Winston logging asynchronous?

After a quick review of the source on github, I'd say that no, winston is not truly async in all aspects. It does emit , but EventEmitter is not async. Methods have an async-style signature (w/ callback ), but are not always async.

Is Winston a middleware?

Usage. express-winston provides middlewares for request and error logging of your express. js application. It uses 'whitelists' to select properties from the request and (new in 0.2.

What is Winston node js?

Winston is the most popular logging library for Node. js. It aims to make logging more flexible and extensible by decoupling different aspects such as log levels, formatting, and storage so that each API is independent and many combinations are supported. It also uses Node.


1 Answers

I have the same issue, seems that the winston-mongodb log function takes different argument ( info as the meta object to be logged into the mongo database, cb is the callback function if you want to see the result after the log operation on the mongo done)

Solution : install the winston-mongodb package with the version no 3.0.0

npm install [email protected] --save

reference to the github issue cb is not a function

like image 159
M.elsayed Avatar answered Oct 23 '22 08:10

M.elsayed