Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate colors if not supported #279

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function printBuffer(buffer, options) {
}

if (logger.withTrace) {
logger.groupCollapsed(`TRACE`);
logger.groupCollapsed('TRACE');
logger.trace();
logger.groupEnd();
}
Expand Down
4 changes: 3 additions & 1 deletion src/defaults.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
81 changes: 81 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 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));
}

export function groupCollapsed(...args) {
console.groupCollapsed(...truncateColorArguments(support.consoleGroupStyles, ...args));
}

export function groupEnd() {
try {
console.groupEnd();
} catch (e) {
log('—— log end ——');
}
}