I use PM2, but I don't care for the logs that much. Instead I use bunyan, which gives a ton of flexibility for logging. If you npm install it with --global you can also use it as a live log viewer:
This won't timestamp your console.log output, though. But If you convert to log.info() or any other Bunyan log function you will get nice logging.
As per the command line help (pm2 logs -h) running pm2 logs --timestamp command should add the timestamp to the logs. However it does seem to not affect old logs! Apparently only new logs show up with timestamp.
To fix this issue pass --log-date-format="YYYY-MM-DD HH:mm Z" to pm2 as a param. For example:
Create an Ecosystem file pm2-config.js in your application root (ex: beside package.json)
Paste the below contents & save:
module.exports = {
apps: [
{
name: "my-app1",
script: "./index.js",
time: true, // <----------------------- This is the key to make it work
watch: false,
env: {
PORT: 4001,
NODE_ENV: "production",
},
},
],
};
Now create a shell script start.sh (OR, batch file, OR, directly run below commands)