From fde527aaeb07425f17c40cc8dc29649e9bd82b07 Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer Date: Sun, 24 Mar 2019 10:06:28 +0100 Subject: [PATCH] Major logging changes --- actions.js | 36 ++++++++++++++++---------------- logger.js | 46 ++++++++++++++++++++++++++++++++++------ server.js | 61 ++++++++++++++---------------------------------------- stat.js | 12 +++++------ utils.js | 6 +++--- 5 files changed, 83 insertions(+), 78 deletions(-) diff --git a/actions.js b/actions.js index 4c10fa8..74b2f7f 100644 --- a/actions.js +++ b/actions.js @@ -221,7 +221,7 @@ class QuestionDB { function Process(d, file) { try { - logger.Log("[PCES]:\tFile: " + file); + logger.Log("File: " + file); if (d.data.split("\n").length > 1) { var oldFile = utils.ReadFile(file); var newFile = oldFile + "\n"; @@ -234,26 +234,26 @@ function Process(d, file) { var oldRes = CheckData(oldFile); if (oldRes.count > 0) - logger.Log("[NLOD]:\t\told public result: " + oldRes.count, logger.GetColor("blue")); + logger.Log("\t\told public result: " + oldRes.count, logger.GetColor("blue")); else - logger.Log("[NLOD]:\t\told public NLOD error, " + oldRes.log, logger.GetColor("redbg"), true); + logger.Log("\t\told public NLOD error, " + oldRes.log, logger.GetColor("redbg"), true); if (newRes.count > 0) - logger.Log("[NLOD]:\t\tnew file result: " + newRes.count, logger.GetColor("blue")); + logger.Log("\t\tnew file result: " + newRes.count, logger.GetColor("blue")); else - logger.Log("[NLOD]:\t\tnew file NLOD error, " + newRes.log, logger.GetColor("redbg"), true); + logger.Log("\t\tnew file NLOD error, " + newRes.log, logger.GetColor("redbg"), true); utils.WriteFile(newFile, file); - logger.Log("[NLOD]:\t\tNew data written to: " + file); + logger.Log("\t\tNew data written to: " + file); return newRes.count - oldRes.count; } else - logger.Log("[PCES]:\t\tNo new data"); + logger.Log("\t\tNo new data"); } catch (e) { Beep(); - logger.Log("[ERR ]:\tError at processing data! File: " + file, logger.GetColor("redbg")); + logger.Log("\tError at processing data! File: " + file, logger.GetColor("redbg")); logger.Log(e.toString(), logger.GetColor("redbg")); } return -1; @@ -262,7 +262,7 @@ function Process(d, file) { function ProcessIncomingRequest(data) { if (data == undefined) { - logger.Log("[PCES]:\tRecieved data is undefined!", logger.GetColor("redbg")); + logger.Log("\tRecieved data is undefined!", logger.GetColor("redbg")); return; } @@ -291,7 +291,7 @@ function ProcessIncomingRequest(data) { data.AddQuestion(d.subj, q); } - logger.Log("[PCES]:\t" + d.subj); + logger.Log("\t" + d.subj); var msg = "All / new count: " + allQuestions.length + " / " + questions.length; if (d.version != undefined) msg += ". Version: " + d.version; @@ -310,10 +310,10 @@ function ProcessIncomingRequest(data) { msg += " - Data file written!"; var color = logger.GetColor("blue"); } - logger.Log("[PCES]:\t" + msg, color); + logger.Log("\t" + msg, color); } catch (e) { - logger.Log("[PCES]: Couldnt parse JSON data, trying old format...", logger.GetColor("redbg")); + logger.Log("Couldnt parse JSON data, trying old format...", logger.GetColor("redbg")); var d = SetupData(data); var qcount = -1; try { @@ -325,7 +325,7 @@ function ProcessIncomingRequest(data) { qcount = count; } catch (e) {console.log("Error :c"); console.log(e);} - logger.Log("[PCES]:\tProcessing data: " + d.subj + " (" + d.type + "), count: " + qcount, logger.GetColor("green")); + logger.Log("\tProcessing data: " + d.subj + " (" + d.type + "), count: " + qcount, logger.GetColor("green")); if (d.subj == undefined){ logger.Log(JSON.stringify(d), logger.GetColor("red")); return; @@ -346,14 +346,14 @@ function PrintNewCount(d, newItems, file) { for (var i = 0; i < splitted.length; i++) if (splitted[i].startsWith("?")) count++; - logger.Log("[NEW ]:\t" + file + " All / New: " + count + " / " + newItems, logger.GetColor("cyan")); + logger.Log("\t" + file + " All / New: " + count + " / " + newItems, logger.GetColor("cyan")); } } function SetupData(data) { var pdata = data.split("<#>"); if (pdata.length <= 0){ - logger.Log("[SUPD]: Data length is zero !", logger.GetColor("redbg")); + logger.Log("Data length is zero !", logger.GetColor("redbg")); throw "No data recieved!"; } @@ -363,7 +363,7 @@ function SetupData(data) { if (td.length == 2) d[td[0]] = td[1]; else { - logger.Log("[SUPD]: Invalid parameter!", logger.GetColor("redbg")); + logger.Log("Invalid parameter!", logger.GetColor("redbg")); throw "Invalid parameter recieved!"; } } @@ -375,7 +375,7 @@ function CheckData(data) { var presult = NLoad(data); return presult; } catch (e) { - logger.Log("[NLOD]: NLOD error, " + e.toString(), logger.GetColor("redbg"), true); + logger.Log("Load error, " + e.toString(), logger.GetColor("redbg"), true); return { count: -1, log: [e.toString()] @@ -500,7 +500,7 @@ function LoadJSON(resource) { } return r; } catch (e) { - logger.Log("[LOAD]:Error loading sutff", logger.GetColor("redbg"), true); + logger.Log("Error loading sutff", logger.GetColor("redbg"), true); } } diff --git a/logger.js b/logger.js index 2ae4886..884698d 100644 --- a/logger.js +++ b/logger.js @@ -21,13 +21,17 @@ module.exports = { GetDateString: GetDateString, Log: Log, - GetColor: GetColor + GetColor: GetColor, + LogReq: LogReq }; +const DELIM = "|"; + var utils = require('./utils.js'); const nlogFile = "stats/nlogs"; -const varLogFile = "/var/log/qminingNLog"; const logFile = "/nlogs/nlogs"; +const locLogFile = "stats/logs"; +const allLogFile = "/nlogs/log"; function GetDateString() { var m = new Date(); @@ -41,15 +45,45 @@ function GetDateString() { function Log(s, c, b) { if (c != undefined) - console.log(c, GetDateString() + "> " + s); + console.log(c, GetDateString() + DELIM + s); else - console.log(GetDateString() + "> " + s); + console.log(GetDateString() + DELIM + s); if (b) utils.Beep(); - utils.AppendToFile(GetDateString() + "> " + s, nlogFile); - utils.AppendToFile(GetDateString() + "> " + s, logFile); + utils.AppendToFile(GetDateString() + DELIM + s, nlogFile); + utils.AppendToFile(GetDateString() + DELIM + s, logFile); +} + +function LogReq(req, toFile, sc) { + try { + var ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress; + var logEntry = ip + DELIM + req.headers['user-agent'] + + DELIM + req.method + DELIM; + + logEntry += req.url; + + if (sc != undefined && sc == 404) + logEntry += DELIM + sc; + var color = GetColor("green"); + + if (req.url.toLowerCase().includes("isadding")) + color = GetColor("yellow"); + if (!toFile) { + Log(logEntry, color); + } else { + var defLogs = GetDateString() + DELIM + logEntry; + var extraLogs = "\n\t" + JSON.stringify(req.headers) + "\n\t" + JSON.stringify(req.body) + "\n"; + + utils.AppendToFile(defLogs, locLogFile); + utils.AppendToFile(defLogs, allLogFile); + } + + } catch (e) { + console.log(e); + Log("Error at logging lol", GetColor("redbg"), true); + } } function GetColor(c) { diff --git a/server.js b/server.js index 2902995..64b71a9 100644 --- a/server.js +++ b/server.js @@ -44,7 +44,6 @@ const simpOutFile = "public/simplified"; const inputFile = "stats/inputs"; const msgFile = "stats/msgs"; const logFile = "stats/logs"; - const allLogFile = "/nlogs/log"; // https://certbot.eff.org/ @@ -77,7 +76,7 @@ const httpsPort = 8443; app.set('view engine', 'ejs'); app.use(function(req, res, next) { res.on('finish', function() { - Log(req, true, res.statusCode); + logger.LogReq(req, true, res.statusCode); if (res.statusCode != 404) stat.LogStat(req.url); }); @@ -114,7 +113,7 @@ app.get('/sio', function(req, res) { app.get('/manual', function(req, res) { res.write(utils.ReadFile(manFile)); res.end(); - Log(req); + logger.LogReq(req); }); app.get('/public', function(req, res) { @@ -147,12 +146,12 @@ app.get('/legacy', function(req, res) { siteurl: siteUrl }); - Log(req); + logger.LogReq(req); }); app.post('/postfeedback', function(req, res) { res.redirect('back'); - logger.Log("[UMSG]: New feedback message", logger.GetColor("bluebg"), true); + logger.Log("New feedback message", logger.GetColor("bluebg"), true); utils.AppendToFile("\n\n" + logger.GetDateString() + ": " + req.body.message_field, msgFile); }); @@ -166,7 +165,7 @@ app.get('/postfeedback', function(req, res) { app.post('/isAdding', function(req, res) { res.end('OK'); - Log(req); + logger.LogReq(req); actions.ProcessIncomingRequest(req.body.datatoadd); utils.WriteBackup(); }); @@ -174,19 +173,19 @@ app.post('/isAdding', function(req, res) { app.get('/lred', function(req, res) { res.redirect("/legacy"); res.end(); - Log(req); + logger.LogReq(req); }); app.get('/menuClick', function(req, res) { res.redirect("/"); res.end(); - Log(req); + logger.LogReq(req); }); app.get('/irc', function(req, res) { res.redirect(ircURL); res.end(); - Log(req); + logger.LogReq(req); }); // all questions readable @@ -197,32 +196,32 @@ app.get('/allqr', function(req, res) { res.render('allqr', { d: d.toString().split('\n') }); - Log(req); + logger.LogReq(req); }); app.get('/greasy', function(req, res) { res.redirect("https://greasyfork.org/en/scripts/38999-moodle-elearning-kmooc-test-help"); res.end(); - Log(req); + logger.LogReq(req); }); app.get('/scriptgit', function(req, res) { res.redirect("https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript"); res.end(); - Log(req); + logger.LogReq(req); }); app.get('/servergit', function(req, res) { res.redirect("https://gitlab.com/YourFriendlyNeighborhoodDealer/question-node-server"); res.end(); - Log(req); + logger.LogReq(req); }); function UploadFile(req, res, path, next) { var fstream; req.pipe(req.busboy); req.busboy.on('file', function(fieldname, file, filename) { - logger.Log("[UPL]: Uploading: " + filename, logger.GetColor("blue")); + logger.Log("Uploading: " + filename, logger.GetColor("blue")); utils.CreatePath(path, true); let d = new Date(); @@ -233,7 +232,7 @@ function UploadFile(req, res, path, next) { fstream = fs.createWriteStream(path + "/" + fn); file.pipe(fstream); fstream.on('close', function() { - logger.Log("[UPL]: Upload Finished of " + path + "/" + fn, logger.GetColor("blue")); + logger.Log("Upload Finished of " + path + "/" + fn, logger.GetColor("blue")); next(fn); }); fstream.on('error', function(err) { @@ -253,7 +252,7 @@ app.route('/badtestsender').post(function(req, res, next) { UploadFile(req, res, recivedFiles, (fn) => { res.render("uploaded"); }); - Log(req); + logger.LogReq(req); }); app.get('*', function(req, res) { @@ -282,32 +281,4 @@ if (startHTTPS && certsLoaded) { logger.Log("Cert files does not exists, starting http only!", logger.GetColor("redbg")); } logger.Log(msg, logger.GetColor("yellow")); -logger.Log("[STRT]: Node version: " + process.version); - -function Log(req, toFile, sc) { - try { - var ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress; - var logEntry = "[RSND]: " + ip + ", " + req.headers['user-agent'] + - " " + req.method + " "; - if (sc != undefined && sc == 404) - logEntry += sc + " "; - logEntry += req.url; - var color = logger.GetColor("green"); - - if (req.url.toLowerCase().includes("isadding")) - color = logger.GetColor("yellow"); - if (!toFile) { - logger.Log(logEntry, color); - } else { - var defLogs = logger.GetDateString() + ": " + logEntry; - var extraLogs = "\n\t" + JSON.stringify(req.headers) + "\n\t" + JSON.stringify(req - .body) + "\n"; - - utils.AppendToFile(defLogs, logFile); - utils.AppendToFile(defLogs, allLogFile); - } - - } catch (e) { - logger.Log("Error at logging lol", logger.GetColor("redbg"), true); - } -} +logger.Log("Node version: " + process.version); diff --git a/stat.js b/stat.js index 8ced883..96bc53c 100644 --- a/stat.js +++ b/stat.js @@ -39,7 +39,7 @@ function Load() { var prevData = utils.ReadFile(statFile); data = JSON.parse(prevData); } catch (e) { - logger.Log("[STAT]: Error at loading logs! (@ first run its normal)", logger.GetColor("redbg")); + logger.Log("Error at loading logs! (@ first run its normal)", logger.GetColor("redbg")); console.log(e); } @@ -47,7 +47,7 @@ function Load() { var prevVData = utils.ReadFile(vStatFile); vData = JSON.parse(prevVData); } catch (e) { - logger.Log("[STAT]: Error at loading visit logs! (@ first run its normal)", logger.GetColor("redbg")); + logger.Log("Error at loading visit logs! (@ first run its normal)", logger.GetColor("redbg")); console.log(e); } } @@ -95,16 +95,16 @@ function Save() { if (writes == writeInterval) { try { utils.WriteFile(JSON.stringify(data), statFile); - // logger.Log("[STAT] Stats wrote."); + // logger.Log("Stats wrote."); } catch (e) { - logger.Log("[STAT]: Error at writing logs!", logger.GetColor("redbg")); + logger.Log("Error at writing logs!", logger.GetColor("redbg")); console.log(e); } try { utils.WriteFile(JSON.stringify(vData), vStatFile); - // logger.Log("[STAT] Stats wrote."); + // logger.Log("Stats wrote."); } catch (e) { - logger.Log("[STAT]: Error at writing visit logs!", logger.GetColor("redbg")); + logger.Log("Error at writing visit logs!", logger.GetColor("redbg")); console.log(e); } writes = 0; diff --git a/utils.js b/utils.js index 81ef830..e3f736b 100644 --- a/utils.js +++ b/utils.js @@ -59,7 +59,7 @@ function WriteFileAsync(content, path) { CreatePath(path); fs.writeFile(path, content, function(err) { if (err) { - logger.Log("[ERR ]: Error writing file: " + path + " (sync)", logger.GetColor("redbg")); + logger.Log("Error writing file: " + path + " (sync)", logger.GetColor("redbg")); } }); } @@ -68,7 +68,7 @@ function AppendToFile(data, file) { CreatePath(file); fs.appendFile(file, "\n" + data, function(err) { if (err) - logger.Log("[ERR ]: Error writing log file: " + file + " (sync)", logger.GetColor("redbg")); + logger.Log("Error writing log file: " + file + " (sync)", logger.GetColor("redbg")); }); } @@ -84,7 +84,7 @@ function WriteBackup() { try { WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString()); } catch (e) { - logger.Log("[ERR ]: Error backing up data json file!", logger.GetColor("redbg")); + logger.Log("Error backing up data json file!", logger.GetColor("redbg")); console.log(e); } }