NodeJS - Appending events to the console.log
by
Jeremy Canfield |
Updated: March 23 2024
| NodeJS articles
Let's say you have a file named app.js that contains the following.
console.log("Hello World");
If you use the node CLI to run app.js, "Hello World" should be displayed as stdout on your console.
~]$ node app.js
Hello World
On ES6, you could create a constant that contains the timestamp.
const timestamp = () => `[${new Date().toUTCString()}]`
console.log(timestamp() + " Hello World");
Better yet, use backticks to include constant variables without having to use + or , to break in and out of strings and constant variables.
const timestamp = () => `[${new Date().toUTCString()}]`
console.log(`${timestamp()} Hello World`);
The console should then contain the timestamp.
~]$ node app.js
[Mon, 08 May 2023 06:51:27 GMT] Hello World
Did you find this article helpful?
If so, consider buying me a coffee over at