From a7de7e5a2f8b1d642596e29b88547eda1b8d86d7 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 4 Jun 2018 11:39:05 +0200 Subject: [PATCH] Improvements in Logging. AllModules File Implemented. --- BlobStorage.js | 3 +- DebugLog.js | 107 +++++++++++++++++++++++++++++++++++++------------ Root.js | 2 + 3 files changed, 84 insertions(+), 28 deletions(-) diff --git a/BlobStorage.js b/BlobStorage.js index 582b07569..c1ae7c2a4 100644 --- a/BlobStorage.js +++ b/BlobStorage.js @@ -36,7 +36,6 @@ exports.newBlobStorage = function newBlobStorage(BOT) { containerName = pDataOwner.toLowerCase(); logger = DEBUG_MODULE.newDebugLog(); - logger.fileName = MODULE_NAME; logger.bot = bot; logger.fileName = MODULE_NAME + '.' + pDataOwner; logger.initialize(disableLogging); @@ -72,7 +71,7 @@ exports.newBlobStorage = function newBlobStorage(BOT) { catch (err) { logger.write("[ERROR] initialize -> err = " + err.message); - callBack(global.DEFAULT_FAIL_RESPONSE); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/DebugLog.js b/DebugLog.js index 8904751e7..7da4c0bed 100644 --- a/DebugLog.js +++ b/DebugLog.js @@ -48,36 +48,78 @@ function onInizialized(err) { - if (err.result === global.DEFAULT_OK_RESPONSE.result) { + try { - let filePath = thisObject.bot.filePathRoot + "/Logs/" + thisObject.bot.process + "/" + executionDatetime; + if (err.result === global.DEFAULT_OK_RESPONSE.result) { - if (thisObject.bot.debug.year !== undefined) { + /* We save a file for the module doing the logging at this instance of DebugLog. */ - filePath = filePath + "/" + thisObject.bot.debug.year + "/" + thisObject.bot.debug.month; - } + let filePath = thisObject.bot.filePathRoot + "/Logs/" + thisObject.bot.process + "/" + executionDatetime; - if (loopCounter === undefined) { loopCounter = 0 }; + if (thisObject.bot.debug.year !== undefined) { - filePath = filePath + "/Loop." + pad(loopCounter, 5); - - cloudStorage.createTextFile(filePath, thisObject.fileName + ".json", blobContent + '\n' + "]", onFileCreated); - - function onFileCreated(err) { - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - console.log("[ERROR] Debug-Log -> onLoopFinished -> onInizialized -> onFileCreated -> err = " + err.message); - return; + filePath = filePath + "/" + thisObject.bot.debug.year + "/" + thisObject.bot.debug.month; } + + if (loopCounter === undefined) { loopCounter = 0 }; + + filePath = filePath + "/Loop." + pad(loopCounter, 5); + + cloudStorage.createTextFile(filePath, thisObject.fileName + ".json", blobContent + '\r\n' + "]", onFileCreated); + + function onFileCreated(err) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + console.log("[ERROR] DebugLog -> onLoopFinished -> onInizialized -> onFileCreated -> err = " + err.message); + return; + } + } + + /* We save a single file for the whole Process of the bot, inlcuding AACloud modules. */ + + if (thisObject.bot.sharedLogFileMap !== undefined) { + + let fileSaved = thisObject.bot.sharedLogFileMap.get(loopCounter); + + if (fileSaved !== true) { + + thisObject.bot.sharedLogFileMap.set(loopCounter, true); + + let sharedFileContent = "["; + + for (let i = 0; i < thisObject.bot.blobContent.length; i++) { + + sharedFileContent = sharedFileContent + thisObject.bot.blobContent[i]; + } + + sharedFileContent = sharedFileContent + '\r\n' + "]"; + + cloudStorage.createTextFile(filePath, "AllModules" + ".json", sharedFileContent, onSharedFileCreated); + + function onSharedFileCreated(err) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + console.log("[ERROR] DebugLog -> onLoopFinished -> onInizialized -> onSharedFileCreated -> err = " + err.message); + return; + } + } + } + + } else { + console.log("[ERROR] DebugLog -> onLoopFinished -> onInizialized -> err = " + err.message); + } + + } else { + console.log("[ERROR] DebugLog -> onLoopFinished -> onInizialized -> sharedLogFileMap is undefined. "); } - } else { - console.log("[ERROR] Debug-Log -> onLoopFinished -> onInizialized -> err = " + err.message); + } catch (err) { + console.log("[ERROR] DebugLog -> onLoopFinished -> onInizialized -> err = " + err.message); } } } catch (err) { - console.log("[ERROR] Debug-Log -> onLoopFinished -> err = " + err.message); + console.log("[ERROR] DebugLog -> onLoopFinished -> err = " + err.message); } } @@ -99,7 +141,7 @@ loopCounter = thisObject.bot.loopCounter; - blobContent = "["; + blobContent = "["; // Local version } } else { @@ -115,13 +157,9 @@ messageId++; - let line = { - date: newDate, - sec: messageId, - data: Message - }; + /* When writing one file pero module we need this. */ - let fileLine = '\r\n' + JSON.stringify(line); + let fileLine = '\r\n' + "['" + newDate + "'," + messageId + ",'" + Message + "']"; if (blobContent === "[") { @@ -131,8 +169,25 @@ blobContent = blobContent + "," + fileLine; } + + /* When writting one file for all modules we use this. */ + + let sharedFileLine; + + if (thisObject.bot.blobContent === undefined || thisObject.bot.blobContent.length === 0) { + + thisObject.bot.blobContent = []; + sharedFileLine = '\r\n' + "['" + newDate + "'," + (thisObject.bot.blobContent.length + 1) + ",'" + thisObject.fileName + "','" + Message + "']"; + thisObject.bot.blobContent.push(sharedFileLine.toString()); + + } else { + sharedFileLine = '\r\n' + "['" + newDate + "'," + (thisObject.bot.blobContent.length + 1) + ",'" + thisObject.fileName + "','" + Message + "']"; + thisObject.bot.blobContent.push("," + sharedFileLine.toString()); + } + + } catch (err) { - console.log("[ERROR] Debug-Log -> write -> err = " + err.message); + console.log("[ERROR] DebugLog -> write -> err = " + err.message); } } diff --git a/Root.js b/Root.js index a4773d5b3..df6835b50 100644 --- a/Root.js +++ b/Root.js @@ -317,6 +317,7 @@ exports.newRoot = function newRoot() { botConfig.process = listItem.process; botConfig.debug = {}; + botConfig.sharedLogFileMap = new Map(); logger = DEBUG_MODULE.newDebugLog(); logger.fileName = MODULE_NAME; @@ -414,6 +415,7 @@ exports.newRoot = function newRoot() { let padMonth = pad(month, 2) let newInstanceBotConfig = JSON.parse(JSON.stringify(botConfig)); + newInstanceBotConfig.sharedLogFileMap = new Map(); let timeDelay = Math.random() * 10 * 1000; // We introduce a short delay so as to not overload the machine. setTimeout(startProcess, timeDelay);