Skip to content

Instantly share code, notes, and snippets.

@legend80s
Created March 10, 2017 05:33
Show Gist options
  • Save legend80s/5547dd05b338193d33ae1ffd3fa19042 to your computer and use it in GitHub Desktop.
Save legend80s/5547dd05b338193d33ae1ffd3fa19042 to your computer and use it in GitHub Desktop.
console with header
/* eslint-disable no-console */
/**
* console with header,only method `log` is implemented
*
* @example
* const console = consoleWithHeader('[OpenSearch]');
* console.log('App Type is %s', 'Standard'); // outpu:App type is Standard
*
* @param {String} header 统一标示,常用来表示 log 来源
* @param {String} separator header 和 log 主体分隔符
*
* @return {Function} 返回 带 header 的 log 的函数
*/
export default function consoleWithHeader(header = '', separator = ' ') {
// 防止 console 被外部 console 覆盖,导致调用到重写的 console
// const console = consoleWithHeader('[OpenSearch]');
// console.log('App Type is %s', 'Standard'); // 报错:Maximum call stack size exceeded
const nativeConsole = console;
return {
log(...args) {
const [arg0, ...otherArgs] = args;
nativeConsole.log(...[`${header}${separator}${arg0}`, ...otherArgs]);
},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment