What is the best way to create a logger file in Node js project?

Viewed 60

I found two ways to create a logger file in the Node js project.

  1. We can create a local logs file that can store every log for the specified expiration time.
var errLog = `Error :${title} \n ${text} \n \n TimeStamp : ${new Date()} \n ============================== \n\n`;
let dateString = new Date().toISOString();
this.fs.appendFile(`logs/${dateString.split('T')[0]}_error.log`, errLog, function (err) {});

This is how I am using a local logger to store logs.

  1. Another way is to store all logs in the database with expiration time. It can store some specific details like size, data, status, etc. for each and every log.

If there were any other better ways then please share them.

0 Answers
Related