# Node isn't logging error messages to MongoDB as expected

**URL:** <https://forum.codewithmosh.com/t/node-isnt-logging-error-messages-to-mongodb-as-expected/3625>\
**Category:** Node\
**Created:** [March 27, 2021, 11:56pm UTC](https://forum.codewithmosh.com/t/node-isnt-logging-error-messages-to-mongodb-as-expected/3625 "2021-03-27T23:56:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pfabbi](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@pfabbi](https://forum.codewithmosh.com/u/pfabbi)\
**Post date:** [March 27, 2021, 11:56pm UTC](https://forum.codewithmosh.com/t/node-isnt-logging-error-messages-to-mongodb-as-expected/3625/1 "2021-03-27T23:56:33Z")

</div>

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

```auto
require("winston-mongodb");

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

```

genres.js

```auto
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](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/3/34832abcb032881a3b4baddc09906a273b62dcc4.png)

But this is what I keep getting:

 ![logging_to_mongodb](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/8/863a352d140ba7868fbf88c3b6e21d1b702f5d75.png)

**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`

---

<div class="post-metadata">

**Author:** ![sufian.babri](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sufian.babri/32/1028_2.png) [@sufian.babri](https://forum.codewithmosh.com/u/sufian.babri)\
**Post date:** [April 14, 2021, 3:34pm UTC](https://forum.codewithmosh.com/t/node-isnt-logging-error-messages-to-mongodb-as-expected/3625/2 "2021-04-14T15:34:53Z")

</div>

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](https://github.com/SufianBabri/moviesdb-server/blob/main/startup/logging.js) if you plan to switch to it.

---

<div class="post-metadata">

**Author:** ![luanpersini](https://avatars.discourse-cdn.com/v4/letter/l/71e660/32.png) [@luanpersini](https://forum.codewithmosh.com/u/luanpersini)\
**Post date:** [May 17, 2021, 1:50am UTC](https://forum.codewithmosh.com/t/node-isnt-logging-error-messages-to-mongodb-as-expected/3625/3 "2021-05-17T01:50:16Z")

</div>

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

    });
```

---

<div class="post-metadata">

**Author:** ![pritamaber](https://avatars.discourse-cdn.com/v4/letter/p/6f9a4e/32.png) [@pritamaber](https://forum.codewithmosh.com/u/pritamaber)\
**Post date:** [June 26, 2021, 2:42pm UTC](https://forum.codewithmosh.com/t/node-isnt-logging-error-messages-to-mongodb-as-expected/3625/4 "2021-06-26T14:42:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gabyreload](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/gabyreload/32/2072_2.png) [@gabyreload](https://forum.codewithmosh.com/u/gabyreload)\
**Post date:** [August 30, 2021, 10:45am UTC](https://forum.codewithmosh.com/t/node-isnt-logging-error-messages-to-mongodb-as-expected/3625/5 "2021-08-30T10:45:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gabyreload](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/gabyreload/32/2072_2.png) [@gabyreload](https://forum.codewithmosh.com/u/gabyreload)\
**Post date:** [August 30, 2021, 10:53am UTC](https://forum.codewithmosh.com/t/node-isnt-logging-error-messages-to-mongodb-as-expected/3625/6 "2021-08-30T10:53:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AnthonyMJJones](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@AnthonyMJJones](https://forum.codewithmosh.com/u/AnthonyMJJones)\
**Post date:** [September 1, 2021, 11:50am UTC](https://forum.codewithmosh.com/t/node-isnt-logging-error-messages-to-mongodb-as-expected/3625/7 "2021-09-01T11:50:08Z")

</div>

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???

---

<div class="post-metadata">

**Author:** ![mody1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/mody1/32/4241_2.png) [@mody1](https://forum.codewithmosh.com/u/mody1)\
**Post date:** [August 3, 2022, 4:57am UTC](https://forum.codewithmosh.com/t/node-isnt-logging-error-messages-to-mongodb-as-expected/3625/8 "2022-08-03T04:57:14Z")

</div>

@AnthonyMJJones@gabyreload did u figure it out ? xD
