Logging functionality re-written, all modules affected

This commit is contained in:
Luis Fernando Molina committed 2018-06-06 16:27:51 +02:00
1 parent dfefc0ff26
commit 2e28f86fd5
19 files changed
+1362 -1524

No files matched your search

+46 -102
View File
@@ -3,22 +3,19 @@
const ROOT_DIR = './';
const MODULE_NAME = "DebugLog";
let executionDatetime = "Y." + global.EXECUTION_DATETIME.getUTCFullYear() +
".M." + pad(global.EXECUTION_DATETIME.getUTCMonth() + 1, 2) +
".D." + pad(global.EXECUTION_DATETIME.getUTCDate(), 2) +
".H." + pad(global.EXECUTION_DATETIME.getUTCHours(),2) +
".M." + pad(global.EXECUTION_DATETIME.getUTCMinutes(),2) +
".S." + pad(global.EXECUTION_DATETIME.getUTCSeconds(),2);
let executionDatetime = "D." + global.EXECUTION_DATETIME.getUTCFullYear() +
"." + pad(global.EXECUTION_DATETIME.getUTCMonth() + 1, 2) +
"." + pad(global.EXECUTION_DATETIME.getUTCDate(), 2) +
".T." + pad(global.EXECUTION_DATETIME.getUTCHours(),2) +
"." + pad(global.EXECUTION_DATETIME.getUTCMinutes(),2) +
"." + pad(global.EXECUTION_DATETIME.getUTCSeconds(),2);
let messageId = 0;
let loopCounter;
let loopIncremented = false;
let thisObject = {
bot: undefined,
fileName: undefined,
forceLoopSplit: false, // When set to 'true' this will force that the logs of the current module are split in many different Loop folders.
write: write,
persist: persist,
initialize: initialize
};
@@ -34,19 +31,24 @@
disableLogging = pdisableLogging
if (disableLogging !== true) {
thisObject.bot.eventHandler.listenToEvent("Close Log File", onLoopFinished);
}
} catch (err) {
console.log("[ERROR] DebugLog -> initialize -> err = " + err.message);
console.log("[ERROR] Debug Log -> initialize -> err = " + err.message);
}
}
function onLoopFinished(event) {
function persist() {
/* Here we actually write the content of the in-memory log to a blob */
try {
if (global.CURRENT_EXECUTION_AT === "Browser") {
console.log("[INFO] DebugLog -> persist -> We do not persist logs while running at the Browser");
return;
}
const BLOB_STORAGE = require(ROOT_DIR + 'BlobStorage');
let cloudStorage = BLOB_STORAGE.newBlobStorage(thisObject.bot);
@@ -58,8 +60,6 @@
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
/* We save a file for the module doing the logging at this instance of DebugLog. */
let filePath = thisObject.bot.filePathRoot + "/Logs/" + thisObject.bot.process + "/" + executionDatetime;
if (thisObject.bot.debug.year !== undefined) {
@@ -67,135 +67,79 @@
filePath = filePath + "/" + thisObject.bot.debug.year + "/" + thisObject.bot.debug.month;
}
if (loopCounter === undefined) { loopCounter = 0 };
let fileName = "Loop." + pad(thisObject.bot.loopCounter, 8) + ".json";
filePath = filePath + "/Loop." + pad(loopCounter, 5);
cloudStorage.createTextFile(filePath, thisObject.fileName + ".json", blobContent + '\r\n' + "]", onFileCreated);
cloudStorage.createTextFile(filePath, fileName, 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);
console.log("[ERROR] DebugLog -> persist -> onInizialized -> onFileCreated -> err = " + err.message);
console.log("[ERROR] DebugLog -> persist -> onInizialized -> onFileCreated -> filePath = " + filePath);
console.log("[ERROR] DebugLog -> persist -> onInizialized -> onFileCreated -> fileName = " + fileName);
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];
}
thisObject.bot.blobContent = [];
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);
blobContent = "";
thisObject = {};
}
} else {
console.log("[ERROR] DebugLog -> onLoopFinished -> onInizialized -> sharedLogFileMap is undefined. ");
console.log("[ERROR] DebugLog -> persist -> onInizialized -> cloud storge failed to initialize. ");
console.log("[ERROR] DebugLog -> persist -> onInizialized -> err.message = " + err.message);
}
} catch (err) {
console.log("[ERROR] DebugLog -> onLoopFinished -> onInizialized -> err = " + err.message);
console.log("[ERROR] DebugLog -> persist -> onInizialized -> err = " + err.message);
}
}
} catch (err) {
console.log("[ERROR] DebugLog -> onLoopFinished -> err = " + err.message);
console.log("[ERROR] DebugLog -> persist -> err = " + err.message);
}
}
function write(Message) {
function write(pModule, pMessage) {
try {
if (disableLogging === true) { return; }
console.log("AACloud" + spacePad(thisObject.fileName, 50) + " : " + Message);
if (global.CURRENT_EXECUTION_AT === "Browser") {
if (thisObject.bot === undefined) { return; }
/* Only at the browser we send the messages to the console. At the Cloud it does not make sense. */
if (thisObject.bot.loopCounter !== loopCounter) {
console.log("AACloud" + spacePad(pModule, 50) + " : " + pMessage);
return;
}
if (thisObject.forceLoopSplit === false) {
if (global.CURRENT_EXECUTION_AT === "Cloud") {
if (loopIncremented === false) {
/* Only ERRORs go to the cloud console. */
loopIncremented = true;
if (pMessage.indexOf("[ERROR]") >= 0) {
loopCounter = thisObject.bot.loopCounter;
blobContent = "["; // Local version
}
} else {
loopIncremented = true;
loopCounter = thisObject.bot.loopCounter;
console.log("AACloud" + spacePad(pModule, 50) + " : " + pMessage);
}
}
if (thisObject.bot === undefined) { return; }
let newDate = new Date();
newDate = newDate.toISOString();
messageId++;
/* When writing one file pero module we need this. */
let fileLine = '\r\n' + "['" + newDate + "'," + messageId + ",'" + Message + "']";
if (blobContent === "[") {
blobContent = blobContent + fileLine;
} else {
blobContent = blobContent + "," + fileLine;
}
/* When writting one file for all modules we use this. */
let sharedFileLine;
let logLine = '\r\n' + "['" + newDate + "'," + messageId + ",'" + pModule + "','" + pMessage + "']";
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());
}
blobContent = blobContent + logLine;
} catch (err) {
console.log("[ERROR] DebugLog -> write -> err = " + err.message);
}