From e32d3c92339ed8dd0d1204e85c85f7b9706ef750 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 28 Sep 2023 14:13:59 -0400 Subject: [PATCH] app: logging & in-memory sqlite while in non-prod MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tests are currently working on my laptop, but failing on the CI actions. 🤔 There have always been errors on it inserting animals to the database, and now seem like it fails to get routes before the async promise is fufilled? I'm not sure. Switch everything to the bole logger, and not the `console.` stuff. Signed-off-by: Vincent Batts --- app.js | 25 ++++++++++++++----------- config.js | 6 ++++-- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app.js b/app.js index 193a058..9513e68 100644 --- a/app.js +++ b/app.js @@ -4,15 +4,18 @@ const path = require("path"); const morgan = require("morgan"); const bodyParser = require("body-parser"); const sqlite3 = require("sqlite3").verbose(); +const bole = require('bole'); +const log = bole('app'); +const config = require('./config'); const app = express(); // Create an SQLite database and initialize tables -const db = new sqlite3.Database("db/results.db", (err) => { +const db = new sqlite3.Database(config.db_path, (err) => { if (err) { - console.error("Error opening SQLite database:", err.message); + log.error("Error opening SQLite database:", err.message); } else { - console.log("Connected to SQLite database"); + log.info("Connected to SQLite database", config.db_path); db.run(` CREATE TABLE IF NOT EXISTS button_clicks ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -59,16 +62,16 @@ try { [animal, animal], (err) => { if (err) { - console.error(`Error inserting animal ${animal}: `, err.message); + log.error(`Error inserting animal ${animal}: `, err.message); } else { - console.log(`Success inserting animal ${animal}`); + log.info(`Success inserting animal ${animal}`); } }, ); } }); } catch (error) { - console.error("Error loading animals:", error); + log.error("Error loading animals:", error); animals = ["Dog", "Cat", "Elephant", "Lion", "Giraffe"]; } @@ -88,7 +91,7 @@ app.get("/getNextAnimal", async (req, res) => { const randomAnimal = animals[randomIndex]; res.json({ animalName: randomAnimal }); } catch (error) { - console.error("Error fetching random animal:", error); + log.error("Error fetching random animal:", error); res.status(500).json({ error: "Internal server error" }); } }); @@ -98,7 +101,7 @@ app.post("/recordButtonClick", (req, res) => { try { //const { buttonName, sessionId } = req.body; const result = req.body; - console.error(result); + log.error(result); db.run( "INSERT INTO button_clicks (session_id, animal_name, button_name, timestamp, time_difference) VALUES (?, ?, ?, ?, ?)", @@ -111,7 +114,7 @@ app.post("/recordButtonClick", (req, res) => { ], (err) => { if (err) { - console.error("Error recording button click:", err.message); + log.error("Error recording button click:", err.message); res.status(500).json({ error: "Internal server error" }); } else { res.sendStatus(200); @@ -119,7 +122,7 @@ app.post("/recordButtonClick", (req, res) => { }, ); } catch (error) { - console.error("Error recording button click:", error); + log.error("Error recording button click:", error); res.status(500).json({ error: "Internal server error" }); } }); @@ -200,7 +203,7 @@ app.get("/results", async (req, res) => { await Promise.all([getCount, getTotalAvgTime, getAvgTime]); res.json(results); } catch (error) { - console.error("Error fetching results:", error); + log.error("Error fetching results:", error); res.status(500).json({ error: "Internal server error" }); } }); diff --git a/config.js b/config.js index 5b0035d..23f2f77 100644 --- a/config.js +++ b/config.js @@ -9,7 +9,9 @@ config.express = { if (PRODUCTION) { config.express.ip = '0.0.0.0'; - bole.output({ level: 'debug', stream: process.stdout }) -} else { + config.db_path = "db/results.db"; bole.output({ level: 'info', stream: process.stdout }) +} else { + config.db_path = ":memory:"; + bole.output({ level: 'debug', stream: process.stdout }) }