Last active
August 16, 2019 04:17
-
-
Save meldsza/3bd4ee007a95642893ae483a733cc23c to your computer and use it in GitHub Desktop.
Pass pm2 logs to discord via webhook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const webhook_uri = "WEBHOOK URL HERE"; | |
const spawn = require('child_process').spawn; | |
const request = require('request'); | |
var pm2 = false; | |
let queue = []; | |
function createChunks(str) { | |
return str.match(new RegExp('.{1,' + 2000 + '}', 'g')); | |
} | |
function send(data) { | |
if(!data) return; | |
request.post({ | |
url: webhook_uri, | |
form: { | |
content: "```xl\n" + data + "```" | |
} | |
}, function (error, response, body) { | |
//console.log(body); inifite loop | |
}); | |
} | |
function sendData(data) { | |
data = escapeMarkdown(data.toString()); | |
createChunks(data).filter(a => !!a && a != null).map(a => queue.push); | |
} | |
function startlog() { | |
if (pm2 !== false) { | |
console.log('pm2 logs process already started...'); | |
return; | |
} | |
start = false; | |
pm2 = spawn('pm2', ['logs']); | |
pm2.on('exit', (code, signal) => { | |
console.log('PM2 EXIT'); | |
}) | |
pm2.stderr.on('data', (data) => sendData(data)); | |
pm2.stdout.on('data', (data) => sendData(data)); | |
return pm2; | |
}; | |
function escapeMarkdown(text) { | |
return text.replace(/```/g, '`\u200b``'); | |
} | |
startlog(); | |
setInterval(()=>send(queue.shift()),500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment