pm2 live logs

This commit is contained in:
DeSqBlocki committed 2025-12-06 19:15:44 +01:00
1 parent 0b9be294c2
commit ec38d0245a
4 files changed
+69 -4

No files matched your search

+23 -1
View File
@@ -16,7 +16,8 @@
"express-validator": "^7.3.1", "express-validator": "^7.3.1",
"mongodb": "^7.0.0", "mongodb": "^7.0.0",
"morgan": "^1.10.1", "morgan": "^1.10.1",
"nodemon": "^3.1.11" "nodemon": "^3.1.11",
"ws": "^8.18.3"
} }
}, },
"node_modules/@mongodb-js/saslprep": { "node_modules/@mongodb-js/saslprep": {
@@ -1475,6 +1476,27 @@
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
"license": "ISC" "license": "ISC"
},
"node_modules/ws": {
"version": "8.18.3",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.18.3.tgz",
"integrity": "sha512-PEIGCY5tSlUt50cqyMXfCzX+oOPqN0vuGqWzbcJ2xvnkzkq46oOpz7dQaTDBdfICb4N14+GARUDw2XV2N4tvzg==",
"license": "MIT",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
} }
} }
} }
+2 -1
View File
@@ -20,6 +20,7 @@
"express-validator": "^7.3.1", "express-validator": "^7.3.1",
"mongodb": "^7.0.0", "mongodb": "^7.0.0",
"morgan": "^1.10.1", "morgan": "^1.10.1",
"nodemon": "^3.1.11" "nodemon": "^3.1.11",
"ws": "^8.18.3"
} }
} }
+27 -2
View File
@@ -4,6 +4,9 @@ const express = require('express');
const path = require('path'); const path = require('path');
const morgan = require('morgan'); const morgan = require('morgan');
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const http = require('http');
const WebSocket = require('ws');
const { spawn } = require('child_process');
const indexRouter = require('./routes/index'); const indexRouter = require('./routes/index');
@@ -24,7 +27,6 @@ app.use(bodyParser.json());
// skip: (req) => req.url.startsWith('/assets') || req.url.startsWith('/favicon.ico') // skip: (req) => req.url.startsWith('/assets') || req.url.startsWith('/favicon.ico')
// })); // }));
// Routes // Routes
app.use('/', indexRouter); app.use('/', indexRouter);
@@ -37,6 +39,29 @@ app.use((err, req, res, next) => {
}); });
}); });
app.listen(PORT, () => // -----------------------------
// Wrap Express in HTTP + WS
// -----------------------------
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
// When a client connects, stream PM2 logs
wss.on('connection', ws => {
const pm2Logs = spawn('pm2', ['logs', '--json']);
pm2Logs.stdout.on('data', data => {
ws.send(data.toString());
});
pm2Logs.stderr.on('data', data => {
ws.send('ERROR: ' + data.toString());
});
ws.on('close', () => {
pm2Logs.kill();
});
});
server.listen(PORT, () =>
console.log(`Proxer-Downloader running on http://localhost:${PORT}`) console.log(`Proxer-Downloader running on http://localhost:${PORT}`)
); );
+17
View File
@@ -127,10 +127,27 @@
</div> </div>
<!-- ======================= -->
<!-- PM2 LOGS -->
<!-- ======================= -->
<div class="row mt-4">
<div class="col-12">
<pre id="pm2Logs" style="background:#383838; padding:10px; border-radius:5px; height:300px; overflow-y:auto; white-space:pre-wrap; color: #330000;"></pre>
</div>
</div>
<!-- ======================= --> <!-- ======================= -->
<!-- AUTO-REFRESH SCRIPT --> <!-- AUTO-REFRESH SCRIPT -->
<!-- ======================= --> <!-- ======================= -->
<script> <script>
const logBox = document.getElementById("pm2Logs");
const ws = new WebSocket("ws://" + window.location.host);
ws.onmessage = event => {
logBox.textContent += event.data + "\n";
logBox.scrollTop = logBox.scrollHeight;
};
function formatBytes(bytes) { function formatBytes(bytes) {
if (!bytes) return "Unknown"; if (!bytes) return "Unknown";
const units = ["B","KB","MB","GB","TB"]; const units = ["B","KB","MB","GB","TB"];