Logging in nodejs

Hi, i am pretty new to nodejs and i want to log data and keep that data stored in log files on production but console logging bulk of data is causing performance issues on my node server. How can i log data on production and save that data into log files at end of day without affecting performance.

console.log in Node is synchronous and will block the event loop. I’d suggest trimming down what you are logging.

Also, I’d recommend against writing logs to actual log files and instead just use console.log/console.error to write to stdout/stderr, for several reasons:

  • keeping the log routing responsibility out of your application code:
    • keeps the code cleaner
    • makes log routing locations easier to change without deployments
  • scaling applications/containers means it's harder to have control over logfiles
  • scaling applications also means they're more ephemeral, meaning logfiles might not be there depending on the state of the container
  • writing to, say a file or database, over stdout/stderr ties you down to those log targets, takes away your flexibility to pipe the output of stdout/stderr to whatever targets you want, and change this on the fly

I wrote about this much more here: Why should your Node.js application not handle log routing?

1 Like

try “morgan - an npm package”…it’s great and hopefully helps you with the logging…

1 Like

morgan package is best choice for you.

If you want to log http requests I would suggest morgan but if you want to log other things then you should go with winston.