Pm2 changing log file location - node.js

I have couple of questions regarding pm2
How can I change the location of server-error-0.log and
server-out-0.log files location from c:\users\user\.pm2\logs to other drive, due to restriction in server's c drive access.
Can I log the error and info in database instead of a log file? Do I need to write a separate module for that or is there any way to achieve this?

How can I change the location of ...log file location?
To change pm2's log file location, there are 2 solutions: define log path as parameter when pm2 command is executed (-l, -o, -e), or start pm2 from a configuration file.
For the parameter solution, here is an example:
pm2 start app.js -o ./out.log -e ./err.log
If you don't want to define log path every time when pm2 is executed, you can generate a configuration file, define error_file and out_file, and start pm2 from that:
Generate a configuration file: pm2 ecosystem simple. This would generate a file ecosystem.config.js, with following content:
module.exports = {
apps : [{
name : "app1",
script : "./app.js"
}]
}
Define error_file (for error log) and out_file (for info log) in the file, such as:
module.exports = {
apps : [{
name : "app1",
script : "./app.js",
error_file : "./err.log",
out_file : "./out.log"
}]
}
Delete existing processes in pm2:
pm2 delete <pid>
You can get pid by doing:
pm2 status
Start the process from the configuration file:
pm2 start ecosystem.config.js
In this way, the logs are saved to ./err.log and ./out.log.
Please refer to the document for detail information.
Can I log the error and info in database instead of a log file?
I didn't find any resources in official document. It seems you need to write code and save log to database yourself.

Just wanted to add to #shaochuancs answer, that before doing step 3, make sure you delete the old process. If you don't delete the old process, the changes that you made to your process file will not take into effect after you start your app.
You will need to issue this command before doing step 3 above:
pm2 delete <pid>

In case you want pm2 on startup with changed logs path:
pm2 delete all
pm2 start ecosystem.js
pm2 save
pm2 startup

If you want to write both an error log and console log to the same file, might be a use case, like I am interested to have log in OneFile to push to ELK.you can use -l
-l --log [path] specify filepath to output both out and error logs
Here is the example
pm2 start server.js -l /app/logs/server.log
After doing changes do not forget to run this command as mentioned in the answer.
pm2 delete <pid>

Related

pm2 logging pm2.log to JSON

I searched but didn't find it, is it possible to change pm2 logs (specifically pm2.log file, not apps logs) to log in JSON format?
Yes, with apps logs its possible via ecosystem file, but about pm2 itself?
No, But you can log pm2 start into a .log format file
pm2 start server.js --output="/path/to/file.log" --error="/path/to/file.log"
# or
pm2 start server.js --log="/path/to/file.log"
Be aware that writing output and error logs to the same file may increase your effort to search for error entries within the log file.
More detailed information about pm2 log
More detailed information about pm2 log [official site]
example convert .log file to json

Disable PM2 logging(.pm2/pm2.log NOT .pm2/logs)

I have to run a process with PM2 on a device with very limited disk space, so I need to disable all logging otherwise the device will run out of space after several days.
I'm using:
"out_file": "/dev/null",
"error_file": "/dev/null"
It stops PM2 from creating logs for the process. However, PM2 still creates another log file in .pm2/pm2.log
The size of pm2.log can grow up to 9Mb, which is large for the device.
Is there any way to stop PM2 from creating pm2.log? Or at least some way to clean the log file automatically.
I open an issue at https://github.com/Unitech/pm2/issues/2921
Turns out that pm2.log cannot be disabled in current version of PM2(2.4.6), but the maintainer of PM2 said that this feature will be added in future update.
For now, I'm using the following code to clean log files:
var cmd_flush ='pm2 flush';
var exec = require('child_process').exec;
exec(cmd_flush ,function(error){
debugLog('error: ' + error);
});
It's slow, but at least it works.

Write PM2 logs to custom path

I use PM2 to execute my Node.js app.
In order to do that, I have defined the following ecosystem config file:
apps:
- script: app.js
name: "myApp"
exec_mode: cluster
cwd: "/etc/myService/myApp"
Everything is working. Now I want to specify the custom location for the PM2's logs, therefore I added into ecosystem config file:
log: "/etc/myService/myApp/logs/myApp.log"
It works, but I paid attention that after execution of pm2 start ecosystem, PM2 will write the logs to both locations at the same time:
/etc/myService/myApp/logs/myApp.log (as expected)
/home/%$user%/.pm2/logs/ (default logs destination)
How can I specify the only place for logs of PM2 and avoid the duplicate logs generation?
Based on the comment of robertklep, in order to solve the issue we have to use out_file and err_file fields for output and error log paths respectively.
Syntax sample in YAML format:
out_file: "/etc/myService/myApp/logs/myApp_L.log"
err_file: "/etc/myService/myApp/logs/myApp_E.log"
P.S. The field log can be removed from the config file:
log: "/etc/myService/myApp/logs/myApp.log"

how to detect what triggered PM2 watch restart

Is there some kind of debug option to find out which file exactly was changed and caused PM2 to restart when it's used with --watch key?
At least in my version 4.5.5 it is quite simple - just show the logs. But the trick is that you should not show the log from the process itself. Therefore just execute pm2 logs without specifying the process.
What you will get is something like this:
PM2 | Change detected on path fallback-backend/logger/logfiles/info.log for app SmartProduction-Fallback - restarting

How to reload/restart pm2 with flighplan and be aware of a symlink directory?

I am using flighplan to deploy my web service that built by Node.js
My deployment script uploads the new release to a new directory which has a timestamp or some random characters in its name. I am keeping all my releases in my server so I can rollback easily by just changing the link to any specific release and have zero-downtime deployment.
The main directory, named by the service name and it is just a symbolic link that get changed to the new release's directory after uploading it.
ln -snf ~/tmpDir ~/appName
My problem is when pm2 restars my server it uses the original path of the previous release, it doesn't bind with the symbolic link and follow the link to the new directory that the link is pointing to.
Is there any way to restart or reload pm2 and let it be aware of that symbolic link ?
Short answer - You should not run pm2 inside a symlink which changes.
pm2 would always pick the old path the symlink is pointing to unless you use pm2 kill command.
Solution - create a new directory and make it parent directory of smylink and your code directories (mohmal-144 etc.). For the sake of understanding lets call this deploy.
Now you should have below structure
/home/deploy
/home/deploy/mohmal -> home/deploy/mohmal-144.
If you are using pm2, yous should use ecosystem.json (pm2 config for starting apps) file. Though you can name this file to anything you want. For the sake of understanding, lets call this file ecosystem.json file.
Inside this ecossystem.json file, in the apps section, add the cwd directory and cwd should point to the path of the symlink(not the path to which symlink it pointing to). See below example.
{
"apps": [
{
"name": "mohmal",
"script": "src/bin/server.js",
"exec_mode": "cluster",
"instances" : "0",
"cwd": "/home/deploy/mohmal",
"error_file": "/var/log/mohmal/error.log",
"out_file" : "/var/log/mohmal/out.log",
"merge_logs": true
}
]
}
Place this file in the parent directory which is named deploy in this example.
Now run/use pm2 start, pm2 restart commands from this directory only.
If you are already running pm2 in the system, just once run pm2 kill to clear the old processes.
And then use the suggested changes, you would never have to pm2 kill the processes.
Also the changes to symlink would reflect.

Resources