pm2 logging pm2.log to JSON - node.js

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

Related

Output debug package logs inside a file

I'm using debug npm module to log stuff, is there a way to log into a file programmatically?
Right now I'm doing DEBUG=* node myApp.js > abc.log, how can I log into abc.log by simply running DEBUG=* node myApp.js, while also outputting in stderr?
I didn't find any package doing this.
The package doesn't seem to provide a builtin feature to do this, but it provides you with a hook to customise how logs are emitted.
There is an example in the Readme here.
Note: the example is a bit confusing because it shows you how to replace writing on stdout with ... writing on stdout using the console !
So what you should at the startup of the application:
Open a stream that writes to a file. Tutorial here if you need help on this
Override the log.log() as explained in the doc to write to your file instead of using console.log().

Pm2 changing log file location

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>

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

any body provide me node.js gc log file

Can any body tell me how to redirect output trace_gc to log file in node js instead of console I gave command like this
node -trace_gc -trace_gc_verbose example.js
and also give me one example gc log file for node.js
If you do not need to separate the output of the application itself from the gc trace you can simply redirect the output using:
node -trace_gc -trace_gc_verbose example.js > output.log
Otherwise V8 has a --log_gc option which output to the log file (can be defined with --logfile but this does not seems to output has many information as -trace_gc.
See node --v8-options for all options

Resources