1 year ago
#156242
Omri Hadadi
How to get pm2 logs as json by exec function?
I'm enclosing my code so you can understand my intent, I want to get the last x lines of logs into res.data in my 'pm2exec' function. I run the server by the command 'pm2 start...' in the terminal and then commit an API request.
private **getPm2Logs** = (request : Request , response :Response) => {
// console.log(request.body[0]);
console.log("pid : " + `${request.body.pid}`);
const res : ASYNC_RESPONSE<any> = {success : false};
//const requestBody : PM2_REQUEST = request.body[0]
console.log(request.body);
Pm2Manager.pm2exec(`logs --json --lines ${request.body.lines_count} --nostream `,`${request.body.pid}`)
.then((data : ASYNC_RESPONSE<any>) => {
res.success = data.success;
console.log(res.success);
res.data = data.data; // TODO The logs are not transmitted properly
console.log(res.data);
response.send(res);
})
.catch((catch_data : ASYNC_RESPONSE<any>) => {
res.data = catch_data.data;
response.send(res);
})
}
private pm2exec = (pm2Command : string, pm2Item : string): Promise<ASYNC_RESPONSE<any>> => {
return new Promise<ASYNC_RESPONSE<any>>((resolve, reject) => {
let res: ASYNC_RESPONSE<any> = {success: false};
exec(`pm2 ${pm2Command} ${pm2Item}`, (err, stdout, stderr) => {
if (stderr) {
res.data = {};
reject(res);
} else {
res.success = true;
res.data = JSON.parse(stdout);
resolve(res);
}
});
});
};
logging
pm2
request-promise
0 Answers
Your Answer