From fb674529f4324cfac66e6523c3404d611c491155 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 4 Oct 2017 14:03:59 -0700 Subject: [PATCH] Override console.log in main process, handle non-strings (#1536) This should allow us to get an insight into auto-update behavior and other low-level behaviors happening in the Electron process which would be useful for debugging. FREEBIE --- app/logging.js | 33 +++++++++++++++++++++++++++++++++ js/logging.js | 17 +++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/app/logging.js b/app/logging.js index 34a0ad9f1a8..b4fb40581fd 100644 --- a/app/logging.js +++ b/app/logging.js @@ -83,6 +83,39 @@ function fetch(logPath) { } +function logAtLevel() { + const level = arguments[0]; + const args = Array.prototype.slice.call(arguments, 1); + + if (logger) { + // To avoid [Object object] in our log since console.log handles non-strings smoothly + const str = args.map(function(item) { + if (typeof item !== 'string') { + try { + return JSON.stringify(item); + } + catch (e) { + return item; + } + } + + return item; + }); + logger[level](str.join(' ')); + } else { + console._log.apply(console, consoleArgs); + } +} + + +console._log = console.log; +console.log = _.partial(logAtLevel, 'info'); +console._error = console.error; +console.error = _.partial(logAtLevel, 'error'); +console._warn = console.warn; +console.warn = _.partial(logAtLevel, 'warn'); + + module.exports = { initialize, getLogger, diff --git a/js/logging.js b/js/logging.js index 9d07f1a084a..2253e532c19 100644 --- a/js/logging.js +++ b/js/logging.js @@ -43,8 +43,21 @@ function log() { const consoleArgs = ['INFO ', now()].concat(args); console._log.apply(console, consoleArgs); - const str = redactGroup(redactPhone(args.join(' '))); - ipc.send('log-info', str); + // To avoid [Object object] in our log since console.log handles non-strings smoothly + const str = args.map(function(item) { + if (typeof item !== 'string') { + try { + return JSON.stringify(item); + } + catch (e) { + return item; + } + } + + return item; + }); + const toSend = redactGroup(redactPhone(str.join(' '))); + ipc.send('log-info', toSend); } if (window.console) {