From 42e5e939e90d21afb1dd3f2ad99857a60c4e6c7e Mon Sep 17 00:00:00 2001 From: PhilRunninger Date: Tue, 29 Aug 2023 13:45:18 -0400 Subject: [PATCH] Add some highlighting to the output for increased visibility. --- index.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 2979bd8..3a485f8 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ var options = { }; https.createServer(options, server).listen(process.env.nodeport, () => { - console.log(`${new Date().toISOString()}: Web service started on port ${process.env.nodeport}`) + console.log(`${timestamp()} Web service started on port ${process.env.nodeport}`) }); var Postgres = new pg.Client({ @@ -31,39 +31,47 @@ var Postgres = new pg.Client({ Postgres.connect(); Postgres.FirstRow = function(sql, response) { - console.log(`${new Date().toISOString()}: Running query:\n${sql}`); + console.log(`${timestamp()} Running query:\n${sql}`); Postgres.query(sql, [], (err, res) => { if (err === null) { - console.log(`${new Date().toISOString()}: Reponse: ${JSON.stringify(res.rows[0]).slice(0,100)}${JSON.stringify(res.rows[0]).length<100?'':'… (truncated)'}`); + console.log(`${timestamp()} Reponse: ${JSON.stringify(res.rows[0]).slice(0,100)}${JSON.stringify(res.rows[0]).length<100?'':'… (truncated)'}`); response.json(res.rows[0]); return; } - console.log(`${new Date().toISOString()}: ERROR|${JSON.stringify(err.stack)}`); + console.log(`${timestamp()} ${error()}${JSON.stringify(err.stack)}`); response.json(err.stack); }); }; +function timestamp() { + return `\x1b[33m${new Date().toISOString()}:\x1b[0m` +} + +function error() { + return `\x1b[91mERROR\x1b[0m|` +} + function process_route(route, description, path, res, callback) { - console.log(`${"▼".repeat(120)}`) - console.log(`${new Date().toISOString()}: ${route} (${description})`) + console.log(`\x1b[96m${"▼".repeat(120)}\x1b[0m`) + console.log(`${timestamp()} ${route} (${description})`) if (!path) { callback(undefined); return; } - console.log(`${new Date().toISOString()}: SQL file: ${path}`) + console.log(`${timestamp()} SQL file: ${path}`) fs.readFile(path, 'utf8', function(err, data) { if (!err) { callback(data); } else { - console.log(`${new Date().toISOString()}: ERROR|Could not read sql file: ${JSON.stringify(err)}`); + console.log(`${timestamp()} ${error()}Could not read sql file: ${JSON.stringify(err)}`); res.send('Failed to read needed SQL file.'); } }); } function build_where(req, res) { - console.log(`${new Date().toISOString()}: Building WHERE from ${JSON.stringify(req.body)}`); + console.log(`${timestamp()} Building WHERE from ${JSON.stringify(req.body)}`); var where = ''; for (var column in req.body.scenario) { if (where) { @@ -80,10 +88,10 @@ function build_where(req, res) { if (req.body.stamp) { where = `${where}\n AND order_date >= least('${req.body.stamp}'::date,'2021-06-01')`; } - console.log(`${new Date().toISOString()}: Returning ${where}`); + console.log(`${timestamp()} Returning ${where}`); if (where == '') { - console.log(`${new Date().toISOString()}: ERROR|Unable to create a WHERE clause.`); + console.log(`${timestamp()} ${error()}Unable to create a WHERE clause.`); res.send('No body was sent.'); } @@ -110,7 +118,7 @@ server.get('/get_pool', bodyParser.json(), function(req, res) { function(arg) { if (req.body.quota_rep) { // ensure backward compatibility - console.log(`${new Date().toISOString()}:Converting old format… ${JSON.stringify(req.body)}`); + console.log(`${timestamp()} Converting old format… ${JSON.stringify(req.body)}`); req.body = {'scenario':{'quota_rep_descr':req.body.quota_rep}}; }