From ef9daa0d74854312af423270ec2ca7ddd9e108f7 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 8 Nov 2019 14:34:30 -0500 Subject: [PATCH] refactor to use new logs only instead of event logs --- packages/core/core/src/events.ts | 47 +++++++++++++++---------- packages/core/structlog/src/index.js | 7 ++-- packages/embark/src/lib/core/plugins.js | 8 +++-- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/packages/core/core/src/events.ts b/packages/core/core/src/events.ts index 5d0408b14..2efabc7f5 100644 --- a/packages/core/core/src/events.ts +++ b/packages/core/core/src/events.ts @@ -16,7 +16,6 @@ function warnIfLegacy(eventName: string) { } function getOrigin(override) { - if (!override && !(debugEventsEnabled())) return ""; let origin = ((new Error().stack).split("at ")[3]).trim(); origin = origin.split("(")[0].trim(); return origin; @@ -30,6 +29,7 @@ function log(eventType, eventName, origin?: string) { if (eventName.indexOf("log") >= 0) { return; } + // fs.appendFileSync(".embark/events.log", (new Error().stack) + "\n"); if (!origin && origin !== "") { const stack = new Error().stack; @@ -102,7 +102,7 @@ EmbarkEmitter.prototype.once = function(requestName, cb) { }; EmbarkEmitter.prototype.setHandler = function(requestName, cb) { - log("SET HANDLER", requestName); + // log("SET HANDLER", requestName); warnIfLegacy(requestName); return _setHandler.call(this, requestName, cb); }; @@ -113,13 +113,13 @@ EmbarkEmitter.prototype.request2 = function() { let requestId = this.debugLog.log({parent_id: this.logId, type: "request", name: requestName, inputs: other_args}); - log("\nREQUEST", requestName); + // log("\nREQUEST", requestName); warnIfLegacy(requestName); if (this._events && !this._events['request:' + requestName]) { - this.debugLog.log({id: requestId, error: "no request listener for " + requestName}) - log("NO REQUEST LISTENER", requestName); - if (debugEventsEnabled()) { + if (this.debugLog.isEnabled()) { + this.debugLog.log({ id: requestId, error: "no request listener for " + requestName, source: this.getOrigin()}) + // KEPT for now until api refactor separating requests from commands console.log("made request without listener: " + requestName); console.trace(); } @@ -145,16 +145,18 @@ EmbarkEmitter.prototype.request2 = function() { this._emit('request:' + requestName, ...other_args); }); - const ogStack = (new Error().stack); + let ogStack; + if (this.debugLog.isEnabled()) { + ogStack = (new Error().stack); + } promise.catch((e) => { - if (debugEventsEnabled()) { + if (this.debugLog.isEnabled()) { console.dir(requestName); console.dir(ogStack); } - this.debugLog.log({id: requestId, error: "promise exception", stack: ogStack}) - log("\n======== Exception ========", requestName, "\n " + ogStack + "\n=============="); + this.debugLog.log({id: requestId, error: "promise exception", outputs: ogStack, source: ogStack}) return e; }); @@ -165,14 +167,18 @@ EmbarkEmitter.prototype.request = function() { const requestName = arguments[0]; const other_args = [].slice.call(arguments, 1); - let requestId = this.debugLog.log({parent_id: this.logId, type: "old_request", name: requestName, inputs: other_args}) + let origin; + if (this.debugLog.isEnabled) { + origin = this.getOrigin(); + } - log("\nREQUEST(OLD)", requestName); + let requestId = this.debugLog.log({parent_id: this.logId, type: "old_request", name: requestName, inputs: other_args, source: origin}) + + // log("\nREQUEST(OLD)", requestName); warnIfLegacy(requestName); if (this._events && !this._events['request:' + requestName]) { - this.debugLog.log({id: requestId, error: "no request listener for " + requestName}) - log("NO REQUEST LISTENER", requestName); - if (debugEventsEnabled()) { + if (this.debugLog.isEnabled()) { + this.debugLog.log({id: requestId, error: "no request listener for " + requestName}) console.log("made request without listener: " + requestName); console.trace(); } @@ -197,17 +203,20 @@ EmbarkEmitter.prototype.request = function() { // TODO: ensure that it's only possible to create 1 command handler EmbarkEmitter.prototype.setCommandHandler = function(requestName, cb) { - log("SET COMMAND HANDLER", requestName); + // log("SET COMMAND HANDLER", requestName); let requestId = this.debugLog.log({parent_id: this.logId, type: "setCommandHandler", name: requestName}) // let origin = ((new Error().stack).split("at ")[3]).trim(); // origin = origin.split("(")[0].trim(); - const origin = getOrigin(); + let origin; + if (this.debugLog.isEnabled()) { + origin = this.getOrigin(); + } const listener = function(_cb) { this.debugLog.log({id: requestId, output: origin, source: origin}); - log("== REQUEST RESPONSE", requestName, origin); + // log("== REQUEST RESPONSE", requestName, origin); cb.call(this, ...arguments); }; const listenerName = 'request:' + requestName; @@ -237,7 +246,7 @@ EmbarkEmitter.prototype.setCommandHandler = function(requestName, cb) { // TODO: deprecated/remove this EmbarkEmitter.prototype.setCommandHandlerOnce = function(requestName, cb) { - log("SET COMMAND HANDLER ONCE", requestName); + // log("SET COMMAND HANDLER ONCE", requestName); const listenerName = 'request:' + requestName; diff --git a/packages/core/structlog/src/index.js b/packages/core/structlog/src/index.js index 3849b67a4..092de1c52 100644 --- a/packages/core/structlog/src/index.js +++ b/packages/core/structlog/src/index.js @@ -20,6 +20,11 @@ class SuperLog { this.logfile = "./structlog-" + (processIdentifier || "log") + ".json"; } + // TODO: make this a flag depending on ENV var or constructor + isEnabled() { + return true; + } + addRecord(data) { // DB[data.id] = data fs.appendFileSync(this.logfile, "\n" + stringify(data, jsonFunctionReplacer, 0)); @@ -107,5 +112,3 @@ class SuperLog { } module.exports = SuperLog; - - diff --git a/packages/embark/src/lib/core/plugins.js b/packages/embark/src/lib/core/plugins.js index 58d48cfb0..a38e7ad49 100644 --- a/packages/embark/src/lib/core/plugins.js +++ b/packages/embark/src/lib/core/plugins.js @@ -227,14 +227,16 @@ Plugins.prototype.runActionsForEvent = function(eventName, args, cb, logId) { return cb(null, args); } - this.debugLog.log({parent_id: logId, type: "trigger_action", name: eventName, source: this.events.getOrigin(true), givenLogId: logId, plugins: actionPlugins, inputs: args}); + let origin; + if (this.debugLog.isEnabled()) { + origin = this.events.getOrigin(); + } - this.events.log("ACTION", eventName, ""); + this.debugLog.log({parent_id: logId, type: "trigger_action", name: eventName, source: origin, givenLogId: logId, plugins: actionPlugins, inputs: args}); async.reduce(actionPlugins, args, function (current_args, pluginObj, nextEach) { const [plugin, pluginName] = pluginObj; - self.events.log("== ACTION FOR " + eventName, plugin.name, pluginName); let actionLogId = self.debugLog.log({module: pluginName, type: "action_run", name: (eventName + plugin.name), source: pluginName, inputs: current_args}); if (typeof (args) === 'function') {