From 3ed8339aa3bd68b4eecf1cf2d3ecdb35b4ae8386 Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Thu, 12 May 2022 22:09:08 +0200 Subject: [PATCH] Migrate more console messages to debug() --- lib/www/server/api/index.js | 14 +++++---- lib/www/server/debug.js | 44 +++++++++++++++++++++++++++-- lib/www/server/index.js | 7 +++-- lib/www/server/lib/db/event/post.js | 3 ++ 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/lib/www/server/api/index.js b/lib/www/server/api/index.js index d37d077..ebd8daf 100644 --- a/lib/www/server/api/index.js +++ b/lib/www/server/api/index.js @@ -6,6 +6,7 @@ const cookieParser = require('cookie-parser') const maybeSendAlert = require("../lib/alerts"); const mw = require('./middleware'); +const { ERROR, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename); const verbose = process.env.NODE_ENV != 'test'; const app = express(); app.locals.version = "0.3.1"; // API version @@ -22,7 +23,7 @@ app.map = function(a, route){ } // else drop through // get: function(){ ... } case 'function': - if (verbose) console.log('%s %s', key, route); + if (verbose) INFO('%s %s', key, route); app[key](route, a[key]); break; } @@ -297,10 +298,12 @@ app.use(function (err, req, res, next) { const alert = {title, message, description, error: err}; console.log("Error:", err); + ERROR("%O", err) res.set("Content-Type", "application/json"); if (err instanceof Error && err.name != "UnauthorizedError") { - console.error(err.stack); +// console.error(err.stack); + ERROR(err.stack); res.set("Content-Type", "text/plain"); res.status(500).send('General internal error'); maybeSendAlert(alert); @@ -317,7 +320,7 @@ app.use(function (err, req, res, next) { app.disable('x-powered-by'); app.enable('trust proxy'); -console.log('trust proxy is ' + (app.get('trust proxy')? 'on' : 'off')); +INFO('trust proxy is ' + (app.get('trust proxy')? 'on' : 'off')); const addr = "127.0.0.1"; @@ -325,7 +328,7 @@ if (!module.parent) { var port = process.env.HTTP_PORT || 3000; var server = http.createServer(app).listen(port, addr); - console.log('API started on port ' + port); + INFO('API started on port ' + port); } else { app.start = function (port = 3000, path) { @@ -338,7 +341,8 @@ if (!module.parent) { const server = http.createServer(root).listen(port, addr); if (server) { - console.log(`API started on port ${port}, prefix: ${path || "/"}`); +// console.log(`API started on port ${port}, prefix: ${path || "/"}`); + INFO(`API started on port ${port}, prefix: ${path || "/"}`); } return server; } diff --git a/lib/www/server/debug.js b/lib/www/server/debug.js index b9d56d2..f9e0479 100644 --- a/lib/www/server/debug.js +++ b/lib/www/server/debug.js @@ -1,10 +1,48 @@ const path = require('path'); -function debug (filename) { +function debuggers (filename) { const ns = filename ? "dougal:server:"+path.basename(path.relative(__dirname, filename).replace(/\//g, ':'), path.extname(filename)).replace(/:index$/, "") : "dougal:server"; - return require('debug')(ns); + const debug = require('debug')(ns); + return { + debug, + + // A "panic" condition affecting multiple applications, servers, or sites. + // System is unusable. Notify all technical staff on call. + EMERGENCY: debug.extend("EMERGENCY"), + + // A condition requiring immediate correction or indicating a failure in a + // primary system, for example, a loss of a primary ISP connection. + // Fix CRITICAL issues before ALERT-level problems. + CRITICAL: debug.extend("CRITICAL"), + + // A condition requiring immediate correction, for example, the loss of a + // backup ISP connection. Notify staff who can fix the problem. + ALERT: debug.extend("ALERT"), + + // Non-urgent failures. Notify developers or administrators as errors must + // be resolved within a given time. + ERROR: debug.extend("ERROR"), + + // Warning messages are not errors, but they indicate that an error will occur + // if required action is not taken. An example is a file system that is 85% full. + // Each item must be resolved within a given time. + WARNING: debug.extend("WARNING"), + + // Events that are unusual but are not error conditions. These items might be + // summarized in an email to developers or administrators to spot potential + // problems. No immediate action is required. + NOTICE: debug.extend("NOTICE"), + + // Normal operational messages. These may be harvested for network maintenance + // functions like reporting and throughput measurement. No action is required. + INFO: debug.extend("INFO"), + + // Information useful to developers for debugging an application. This information + // is not useful during operations. + DEBUG: debug.extend("DEBUG"), + }; } -module.exports = debug; +module.exports = debuggers; diff --git a/lib/www/server/index.js b/lib/www/server/index.js index 8d9a65e..ecb377c 100755 --- a/lib/www/server/index.js +++ b/lib/www/server/index.js @@ -1,9 +1,11 @@ #!/usr/bin/node +const { INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename); + async function main () { // Check that we're running against the correct database version const version = require('./lib/version'); - console.log("Running version", await version.describe()); + INFO("Running version", await version.describe()); version.compatible() .then( (versions) => { const api = require('./api'); @@ -17,12 +19,13 @@ async function main () { const eventManagerPath = [__dirname, "events"].join("/"); const eventManager = fork(eventManagerPath, /*{ stdio: 'ignore' }*/); - console.info("Versions", versions); + INFO("Versions:", versions); process.on('exit', () => eventManager.kill()); }) .catch( ({current, wanted, component}) => { console.error(`Fatal error: incompatible ${component} version ${current} (wanted: ${wanted})`); + ERROR(`Fatal error: incompatible ${component} version ${current} (wanted: ${wanted})`); process.exit(1); }); } diff --git a/lib/www/server/lib/db/event/post.js b/lib/www/server/lib/db/event/post.js index 2ee78b2..0adaf55 100644 --- a/lib/www/server/lib/db/event/post.js +++ b/lib/www/server/lib/db/event/post.js @@ -1,3 +1,4 @@ +const { DEBUG, ERROR } = require('DOUGAL_ROOT/debug')(__filename); const { setSurvey, transaction } = require('../connection'); async function post (projectId, payload, opts = {}) { @@ -13,8 +14,10 @@ async function post (projectId, payload, opts = {}) { `; const values = [ p.tstamp, p.sequence, p.point, p.remarks, p.labels ]; + DEBUG("Inserting new values: %O", values); await client.query(text, values); } catch (err) { + err.origin = __filename; throw err; } finally { client.release();