Node isn't logging error messages to MongoDB as expected

I’m trying to create an error message/log using “winston-mongodb” while sending it to my database. In my MongoDB Compass it created a log folder as expected, but there’s no data in it.

Here’s my code:

index.js

require("winston-mongodb");

winston.add(new winston.transports.MongoDB({ db: "mongodb://localhost/vidly_node_js" }));

genres.js

router.get("/", async (req, res) => {
   throw new Error("Could not get the genres.");
   const genres = await Genre.find().sort("name");
   res.send(genres);
});

I’m supposed to be getting this:

logging_to_mongodb_2

But this is what I keep getting:

What am I missing???

Also “throw new Error(“Could not get the genres.”)” is making the code down below in my get function “unreachable”. Maybe that’s the reason? If so, what do I need to do to fix it? Because it’s giving me an error that says:
TypeError: common.clone is not a function

I think I also had a similar issue when I was taking the course last year. I think it was an issue with Winston.

Try using a different logging library. I’m using tracer.

You may refer to my logging.js file if you plan to switch to it.

Hello, show the part of the code where you configured the logging. The Winston library was updated.

Mine is as follow:

const winston = require(‘winston’);
require(‘winston-mongodb’);
require(‘express-async-errors’);
module.exports = function() {

  winston.exceptions.handle(

    new winston.transports.File({ filename: 'uncaughtExceptions.log' }));

   process.on('unhandledRejection', (ex) => {

     throw ex;

   });

  const logger = winston.createLogger({

    level: 'info',

    format: winston.format.combine(              

        winston.format.json(),

        winston.format.timestamp(),   

        //winston.format.prettyPrint(), //Exibe com quebra de linha. Gasta + memória

        //winston.format.simple(),     //Adiciona um Erro em texto simples antes do Json

    ),

    defaultMeta: { service: 'Vidly x111' },

    transports: [   

      //new winston.transports.File({ filename: 'error.log', level: 'error' }),  

      new winston.transports.File({ filename: 'combined.log' }),

      new winston.transports.MongoDB({

        db: "mongodb://localhost/vidly",

        options: {useNewUrlParser: true, useUnifiedTopology: true},

        collection:'logs',

        capped:true,

        size: 5242880,

        level: 'error'              

    }),      

    ],

    meta: true

    });

Maybe this answer wont be relevent in june 2021 to you but other might find it useful so i am posting it …

winston latest version needs —

winston.configure({

transports: [new winston.transports.File({ filename: “logfile.log” })],

});

this code to create log file

This it works for me. But in the logfile, dont recorded the date. Can you help me??
And I cant log to MongoDB

I solved the 2 problems with this:

winston.configure({

transports: [new winston.transports.File({ filename: “logfile.log” })],

transports: [

new winston.transports.MongoDB({ db: "mongodb://localhost/vidly" }),

],

});

Hope Mosh updates the course.

1 Like

I’m a having a couple problems with this. The logfile.log was created and logs the errors but the errors aren’t logged on the node terminal like how Mosh has it and the meta data in MongoDB is empty. Did you have the same problem???

@AnthonyMJJones @gabyreload did u figure it out ? xD