How to add dates to pm2 error logs? - node.js

Is there a way to add timestamps to error logs in .pm2/logs?
I noticed that pm2 logs command shows aggregated logs with timestamps, but looking into log files - there are only messages and stacktraces without dates.

As per the pm2 logs official documentation, you can use --time, which prefixes logs with a standard formatted timestamp.
pm2 start app.js --time
If you have already created the app, you can update it while restarting the application with:
pm2 restart 0 --time
Make sure to pm2 save afterwards.
Note that you can also use a custom formatter as per this issue & this commit:
pm2 start app.js --log-date-format 'DD-MM HH:mm:ss.SSS'
where 'DD-MM HH:mm:ss.SSS' is any momentjs valid format.

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:
pm2 start bin/www --log-date-format="YYYY-MM-DD HH:mm Z"
Using process.json
I like process.json for starting my app for convenience so my process.json contains the following:
{
"apps" : [
{
"name" : "app",
"script" : "bin/www",
"log_date_format" : "YYYY-MM-DD HH:mm Z"
}
]
}
then I start my app by just running:
pm2 start process.json
Once done I see the timestamp showing up just by running:pm2 logs Notice that I didn't have to specify --timestamp to see the timestamp.
app (out): 2016-08-04 13:46 +01:00: My log here
A good read: http://pm2.keymetrics.io/docs/usage/log-management/

pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm"

Wasted 30 mins on this.
Din't work
Other answers din't work
Official CLI din't work too: pm2 start app.js [OPTIONS], ex: pm2 start app.js --time
Worked
Official Ecosystem file worked great (procedure is below)
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)
Paste the below contents & save:
pm2 stop pm2-config.js
pm2 delete pm2-config.js
pm2 start pm2-config.js

To use standard formated timestamp:
pm2 start app.js --time
Or if you want to prefix logs with custom formated timestamp:
pm2 start app.js --log-date-format <format>
Where <format> is a moment display format (eg YYYY-MM-DD HH:mm Z).
And if your app is already running you can use reload for a 0-second-downtime reload:
pm2 reload app.js --time
Or
pm2 reload app.js --log-date-format <format>

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.
To view live pm2 logs with bunyan, just pipe it:
pm2 logs | bunyan

first update the format (make sure server timezone is what you want)
pm2 restart 0 --log-date-format "DD-MM-YYYY HH:MM Z"
save all processes
pm2 save
run these
npm i -G pm2 //if not latest
pm2 update
this works and show log in servers time zone other wise time zone will be different

For process.yml , follow these example format. It Worked for me
apps:
- script : ./SampleApi/app.js
name : 'api-proxy-app'
instances: 2
exec_mode: cluster
watch : true
log_date_format : "YYYY-MM-DD HH:mm Z"
Sample Log format with DateTime:
2019-07-28 13:46 +06:00: channel created for cancel mandate--####################################
2019-07-28 13:46 +06:00: channel created for cancel mandate--####################################
2019-07-28 13:46 +06:00: channel created for exception scenario--####################################
2019-07-28 13:46 +06:00: channel created for create mandate--####################################
2019-07-28 13:46 +06:00: create channel initiated for cancel mandate--------------------->
2019-07-28 13:46 +06:00: create channel initiated for create mandate--------------------->
2019-07-28 13:46 +06:00: create channel initiated for update mandate--------------------->
Using --log-date-format didn't worked for me.

Related

How to get pm2 process to watch after it has been stopped/ restarted?

My pm2 process starts using their default ecosystem file structure:
ecosystem.config.js
module.exports = {
apps: [{
env: {
NODE_ENV: "development"
},
error_file: "./logs/error.log",
ignore_watch: ["logs", "node_modules"],
log_date_format: "YYYY-MM-DD HH:mm:ss Z",
name: "my-app",
out_file: "./logs/output.log",
script: "./server.js",
watch: true
}]
}
I start the process with pm2 start ecosystem.config.js and that works fine, with the app reloading on file changes.
But when I stop the process with pm2 stop ecosystem.config.js, and then start it again with pm2 start ecosystem.config.js, pm2 does not watch for files, despite the display column of watching being enabled.
The only way to start the process up again and have the watch work is to delete the pm2 process, and then start up a new one again.
Am I missing something to make a stop or restart work with watch?
Thanks.
The pm2 watch & restart documentation had the answer (must have glossed over it on first read):
Restart with --watch will toggle the watch parameter.
Looks like omitting that --watch flag on already-existing pm2 instances will not toggle the watch parameter in the ecosystem.config.js file. The watch parameter is only toggled on initial process execution, not subsequent ones.
So stopping the process, then starting again with pm2 start ecosystem.config.js --watch does the trick!
Try adding
watch_options: {
"usePolling": true
}
See here: http://pm2.keymetrics.io/docs/usage/watch-and-restart/
This is not a PM2 specific option, but rather a chokidar option which is used by PM2.
The documentation of those options can be found here.
https://stackoverflow.com/users/7575111/nulldev
watch_options: {
"usePolling": true
}
The answer was helpful to me as a trial environment that doesn't cost me restarting the app every time

Get node console output to file with pm2

How can I get node console output (such as errors) to a file with pm2?
I tried to run pm2 task --log-type json but it didn't work
There is no log file in /var/log or in the project directory.
How can I achieve this?
Just add log property to your ecosystem config file like this:
module.exports = {
apps: [
{
name: name,
script: './server/server.js',
error_file: './logs/err.log',
out_file: './logs/out.log',
log_date_format: 'YYYY-MM-DD HH:mm:ss:SSS',
}
]
}
Then pm2 will automate write log into these file (one for console.log and one for console.error)
Use pm2 list and note process-id for which you want to see the log.
Save the executed process using pm2 save
Then, enter pm2 logs process-id to see the logs and report of respective pm2 process.

Why is PM2 not launching my Node process?

Previously I have had success implementing PM2, but not currently.
My node app does run just fine if I start it manually, but nothing I do gets it to run via PM2, even though PM2 appears to be starting it up. Here's what I mean:
If I run pm2 start server/index.js, the response in the terminal reads:
$ pm2 start server/index.js
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting D:\Program Files\nodeApps\service-management-api\server\index.js in fork_mode (1 instance)
[PM2] Done.*
Then the terminal prints out a table with App info. It doesn't look pretty pasted here so I'll list it out:
App Name: index
id: 0
version: 1.0.0
mode: fork
pid: 8984
status: online
restart: 0
update 0s
cpu: 0%
mem: 26.0 MB
user: ME
watching: disabled
It appears that the node process should be running. But if I immediately enter pm2 list it shows no processes running. If I enter pm2 stop index, it says:
$ pm2 stop index
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][ERROR] Process index not found
Alternatively, if I try using ecosystem.config.js in the commands, I get similar results. Here are the commands tried:
pm2 reload ecosystem.config.js
pm2 start ecosystem.config.js
Example result of running those commmands:
$ pm2 start ecosystem.config.js
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][WARN] Applications sm_api not running, starting...
[PM2] App [sm_api] launched (2 instances)
And CLI prints table showing two instances with status "online" and watching "enabled". And yet, app isn't running (when tested from browser) and "pm2 show " returns:
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][WARN] <app name> doesn't exist
Any clues what's gone awry with my pm2?
Heres my ecosystem.config.js file:
module.exports = {
apps : [{
name: 'sm_api',
script: 'server/index.js',
"log_date_format" : "YYYY-MM-DD HH:mm Z",
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 'max',
error_file : "C:\\pm2_system\\.pm2\\logs\\sm-api-error",
out_file: "C:\\pm2_system\\.pm2\\logs\\sm-api-out",
autorestart: true,
watch: "../",
max_restarts: 10,
max_memory_restart: '1G',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
},
exec_mode: 'cluster'
}],
};
Running in Windows Server 2012 environment. Note that I intend to add pm2-windows-service package after I get pm2 working.
Just a note for those who are saying to run this in no-daemon mode, (pm2 start --no-daemon), you should use this mode if you're trying to diagnose the reason why when you run with the daemon, your script fails.
Almost invariably, this is because you're telling PM2 to watch a dist folder or similar while it's building, so PM2 keeps restarting until it hits its limit (because files are being regenerated at pretty rapid speed) and then outputs the "errored" status.
Running in no-daemon is absolutely not recommended in production. Ensure that PM2 is running as a daemon so it can itself restart, and so it can restart your processes as a process itself. If you kill your terminal sessions or they are automatically timed out on your host, you will quickly find your service dies when that happens.
So, in short.. do it the right way and figure out what the problem is, rather than being lazy and turning PM2 into a glorified wrapper for the node binary.
Ok, I got the answer after posting an issue to the pm2 github issues page.
Sharing it here in case anyone else finds themselves in this situation:
https://github.com/Unitech/pm2/issues/4113
(Basically pm2 3.2.5 introduced a bug that causes this issue in Windows. My dev install was 3.2.4. The issue was resolved by reverting to 3.2.4 in production. Simple process, see instructions at link above.)
As a workaround I used the following:
pm2 start --no-daemon app.js
In my case (pm2 v3.2.2):
pm2 stop all //stop all
rm ./pm2/*.pid //delete all
pm2 start app.config.js

How can I check if my pm2 app NODE_ENV is getting set?

So I just deployed a site with node and pm2 for the first time and I'm going back and doing some optimization and reading best practices, etc.
I read that you can get a lot of benefit by setting NODE_ENV=production.
I found this in the pm2 docs:
[process.json]
"env_production" : {
"NODE_ENV": "production"
}
...
$ pm2 start process.json --env production
So, I did it but I have no idea if it is working. While trying to figure out how to check it I learned to try:
$ node
> process.env.NODE_ENV
> undefined
So, that's not a good sign.. but, with my limited understanding of how the low level stuff works, I can guess that maybe pm2 launches each app as a separate node process? So maybe I'm not in the right process when I try to check it.
Also, I don't know if I have to make a new ~/.pm2/dump.pm2 file because maybe whenever that is maybe overriding the options I set? (because I used pm2 startup).
How do I check if my pm2 app's NODE_ENV is set?
To answer the actual question in the title:
Within your script, for me my Express app's app.js file, you can use process.env.NODE_ENV to get the current value of NODE_ENV and log that out if you want.
An even better way is to use PM2's Process Metrics module, aka pmx.
yarn add pmx
or
npm install pmx --save
then
const Probe = require('pmx').probe()
Probe.metric({
name : 'NODE_ENV',
value : function() {
return process.env.NODE_ENV
}
})
Now it will show up in calls to pm2 monit (bottom left).
To change your environment:
It is necessary that you kill and restart the process to change your environment.
$ pm2 kill && pm2 start pm2.json --env production
The following isn't good enough:
pm2 restart pm2.json --env production
You can also check your NODE_ENV via running pm2 show <yourServerName>. This will output info about your running server including node env.
In addition, you can check your environment variables via running pm2 env 0. This will show all the environment variables for the running node process.
Start it with npm by adding this to your package.json:
"scripts": {
"myScript": "NODE_ENV=production pm2 start server.js"
}
Then
npm start myScript
You can do it directly too, but this is easy to manage, automate wth crontab and is in your source control...
Your process.json file is incomplete. Try using something like this:
[process.json]
{
"name" : "MyApp",
"script" : "myapp.js",
"env_production" : {
"NODE_ENV": "production"
}
}
Then add logging into your code, preferably somwhere on startup:
console.log("NODE_ENV : ", process.env.NODE_ENV);
Now start the application:
pm2 start process.json --env production
Lastly watch app logs:
pm2 logs MyApp
This should do it.
May be at the start of your server script you can print the value of the environment variable and then check the PM2 logs. Use the following code to print your environment variable value:
console.log('process.env.NODE_ENV:', process.env.NODE_ENV);
And then use the following code to see the PM2 logs
pm2 logs app_name
Here app_name is your process name as indicated by the entry in the process.json file.
You can set Environment variable for pm2 specifically.
go to /etc/systemd/system/ location.
you can see a file named pm2-username.service
file. (eg: pm2-root.service ) you can directly add an Enviorment variable for pm2.
for me, it was LD_LIBRARY_PATH . so I added the line as below after the PATH variable.
Environment=PATH=/usr/local/lib......
Environment=LD_LIBRARY_PATH=/opt/oracle/instantclient_21_1
after that, you can restart or start the node application with update-env flag,
pm2 start yourapp --update-env
try pm2 env <app_name/id> also you can find NODE_ENV in pm2 show <app_name/id>
In your terminal just type:
echo NODE_ENV
it will print current selected environment variable

Disable restarting in PM2

I need to stop my nodejs app for some conditions.So I have used process.exit() method. I am running my app with PM2. But PM2 is restarting the app whenever it was stopped. So is there any possibility to disable restarting in PM2.
I think you need the --no-autorestart flag.
"do not automatically restart apps"
http://pm2.keymetrics.io/docs/usage/quick-start/
If you are using pm2 using config json file e.g pm2.config.json or whatever then need to specify similar option "autorestart" to false in your json file.
"autorestart" : false
E.g.
I am using pm2 to run a java application using below json config file
{
"apps" : [
{
"name":"JAVA-Pm2",
"cwd":".",
"script":"/usr/bin/java",
"args":[
"-jar",
"/dir/my-java-app/build/libs/my-java-app-all.jar"
],
"node_args":[],
"exec_interpreter":"",
"exec_mode":"fork",
"autorestart" : false
}
]
}
Simply --no-autorestart works like charm.
Usage example : pm2 start script.js --no-autorestart
If you are using the pm2 config file, use
"autorestart" : false
As suggested by #WitVault BUT make sure you don't have the watch option set to true like so:
"watch": true // This field should NOT be present, otherwise "autorestart" : false is ignored

Resources