addition of new dashboard
This commit is contained in:
40
backend/http/logging.js
Normal file
40
backend/http/logging.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import { getConfig } from "../common/config.js";
|
||||
|
||||
export function logRequest(app) {
|
||||
const config = getConfig();
|
||||
app.use((req, res, next) => {
|
||||
const originalSend = res.send;
|
||||
res.send = function (body) {
|
||||
res.body = body; // Store the response body for logging
|
||||
try {
|
||||
return originalSend.apply(res, arguments); // Proceed with sending the response
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
}
|
||||
};
|
||||
res.on("finish", () => {
|
||||
let output = `${req.method} - ${res.statusCode} - ${req.url}`;
|
||||
if (req.apiMessage) {
|
||||
output += ` - ${req.apiMessage}`;
|
||||
delete req.apiMessage;
|
||||
}
|
||||
console.log(output);
|
||||
if (res.statusCode != 200 || config.debug) {
|
||||
let body;
|
||||
try {
|
||||
body = JSON.parse(res.body);
|
||||
} catch (e) {
|
||||
body = {};
|
||||
}
|
||||
if (body?.error) {
|
||||
console.log(`${req.method} - ${res.statusCode} - ${req.url} - ERROR: ${body.error}`);
|
||||
return;
|
||||
}
|
||||
if (config.debug) {
|
||||
console.log(JSON.stringify(body, null, 2));
|
||||
}
|
||||
}
|
||||
});
|
||||
next();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user