-2

I want to write into a txt file all the logs of my nodeJs program.

I'm using node script-file.js >log-file.txt, but it overwrites the same txt file every time it runs.

Is there a parameter to add so that the title of the generated log-file.txt includes the date and time like this : log-file-18-06-2010-11-57 (dd-mm-yyyy-hh-mm)?

Yacine P
  • 21
  • 4

2 Answers2

2

Thanks to a friend of mine, I was able to answer my question.

As the solution node script.js > log.txt 2> errors.txt generates only one file of logs and one file of errors. Putting >> instead of > permit to append the logs and errors, but the notion of time is lost.

The SOLUTION is to simply do node script.js >> log-error.txt 2>&1 and it generates one file, where both logs and errors are transcribed into this file. You have to console.log the date at the top of the script and it makes a reliable log-errors file. To add the date (it will help beginners) just write this : var today = new Date(); console.log(today.getDate() + '-' + today.getMonth() + '-' + today.getFullYear() + ' ' + today.getHours() + ':' + today.getMinutes() + ':' + today.getSeconds() + '.' + today.getMilliseconds())

Yacine P
  • 21
  • 4
1

How about using package? I think this is very simple way. Here's the awesome log file package

  • I tried the "log file package", but it only writes what we put between brackets. If we put `log('Some data');` we will have a default.log file generated with only "Some data" written in it. I'm looking to copy the logs on the terminal (the console.logs and the errors) on this document – Yacine P Jun 18 '21 at 13:10