Created
March 10, 2017 05:33
-
-
Save legend80s/5547dd05b338193d33ae1ffd3fa19042 to your computer and use it in GitHub Desktop.
console with header
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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