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(); }); }