From fafc7180d376fef73a1f37498e7cf0f4aaf3212e Mon Sep 17 00:00:00 2001 From: Tyler Waters Date: Wed, 20 Dec 2017 14:12:15 -0800 Subject: [PATCH 1/3] (chore) fix linting problems --- src/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core.js b/src/core.js index caee13d..27d6776 100644 --- a/src/core.js +++ b/src/core.js @@ -126,7 +126,7 @@ function printBuffer(buffer, options) { } if (logger.withTrace) { - logger.groupCollapsed(`TRACE`); + logger.groupCollapsed('TRACE'); logger.trace(); logger.groupEnd(); } From 7b221e4994c5829e93b38b59f3241800e33fdefe Mon Sep 17 00:00:00 2001 From: Tyler Waters Date: Wed, 20 Dec 2017 15:06:49 -0800 Subject: [PATCH 2/3] fix: truncate colors if not supported fixes #170 --- src/defaults.js | 4 ++- src/logger.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/logger.js diff --git a/src/defaults.js b/src/defaults.js index 0103ae9..985898d 100644 --- a/src/defaults.js +++ b/src/defaults.js @@ -1,6 +1,8 @@ +import * as logger from './logger'; + export default { level: 'log', - logger: console, + logger: console ? logger : undefined, logErrors: true, collapsed: undefined, predicate: undefined, diff --git a/src/logger.js b/src/logger.js new file mode 100644 index 0000000..c7ff748 --- /dev/null +++ b/src/logger.js @@ -0,0 +1,65 @@ +/* + * Browser support detection heavily borrowed from: + * https://github.com/evgenyrodionov/redux-logger/issues/170#issuecomment-240717892 + */ + +const isNode = typeof exports === 'object' && typeof module !== 'undefined'; + +const browser = { + isNode, + isFirefox: !isNode && /firefox/i.test(navigator.userAgent), + isEdge: !isNode && /\bEdge\b/.test(navigator.userAgent), + isIE: !isNode && !!document.documentMode, + isChrome: !isNode && !!window.chrome, + isSafari: !isNode && !!window.safari, +}; + +const support = { + consoleStyles: !(isNode || browser.isIE || browser.isEdge), + consoleGroupStyles: browser.isChrome || browser.isSafari, +}; + +/** + * Proxies all console calls; if we don't have colors will strip out colors and + * next param(s) that define color information. + * @param {boolean} hasSupport whether we have color support + * @param {*[]} args arguments passed to console method + * @returns {*[]} array of (potentially truncated) arguments to pass back to console method + */ +function truncateColorArguments(hasSupport, ...args) { + if (hasSupport) { return args; } + + const hasColorString = /%c/gi; + + return args.reduce((memo, item, index, array) => { + // if {index} is a string and include %c, remove %c from the string. + // also remove any subsequent params (this would be color information) + if (typeof item === 'string' && hasColorString.test(item)) { + array.splice(index + 1, item.split(hasColorString).length - 1); + memo.push(item.replace(hasColorString, '')); + } else { + memo.push(item); + } + return memo; + }, []); +} + +export function log(...args) { + console.log(...truncateColorArguments(support.consoleStyles, ...args)); +} + +export function group(...args) { + console.group(...truncateColorArguments(support.consoleGroupStyles, ...args)); +} + +export function groupCollapsed(...args) { + console.groupCollapsed(...truncateColorArguments(support.consoleGroupStyles, ...args)); +} + +export function groupEnd() { + try { + console.groupEnd(); + } catch (e) { + log('—— log end ——'); + } +} From aa95bd4dd0b8a2f25544aebd512337dc75e01980 Mon Sep 17 00:00:00 2001 From: Tyler Waters Date: Wed, 20 Dec 2017 16:49:18 -0800 Subject: [PATCH 3/3] fix: Add additional levels --- src/logger.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/logger.js b/src/logger.js index c7ff748..587587c 100644 --- a/src/logger.js +++ b/src/logger.js @@ -44,10 +44,26 @@ function truncateColorArguments(hasSupport, ...args) { }, []); } +export function debug(...args) { + console.debug(...truncateColorArguments(support.consoleStyles, ...args)); +} + export function log(...args) { console.log(...truncateColorArguments(support.consoleStyles, ...args)); } +export function info(...args) { + console.info(...truncateColorArguments(support.consoleStyles, ...args)); +} + +export function warn(...args) { + console.warn(...truncateColorArguments(support.consoleStyles, ...args)); +} + +export function error(...args) { + console.error(...truncateColorArguments(support.consoleStyles, ...args)); +} + export function group(...args) { console.group(...truncateColorArguments(support.consoleGroupStyles, ...args)); }