-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (46 loc) · 1.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict'
const OFFSET = process && parseInt(process.version.slice(1).split('.')[0], 10) > 6 ? 3 : 4
const consoleCaller = (console = ((typeof window !== 'undefined' && window.console) || (typeof global !== 'undefined' && global.console)), LINE_OFFSET = OFFSET) => {
const functionsKeys = Object.keys(console).filter(k => typeof console[k] === 'function')
const COLUMN_OFFSET = 7
const getStackFactory = () => `[${Error().stack.split('\n')[LINE_OFFSET].substr(COLUMN_OFFSET)}]`
const functions = functionsKeys.reduce((acc, el) => {
acc[el] = console[el]
return acc
}, Object.create(null))
functionsKeys.forEach(k => {
switch (k) {
case 'log':
case 'info':
case 'trace':
case 'debug':
case 'warn':
case 'error':
console[k] = (...args) => {
args = [...args, `{caller: ${getStackFactory()}}`]
return functions[k].apply(console, args)
}
break
case 'table':
console[k] = (...args) => {
return functions.log(`{caller: ${getStackFactory()}}`) || functions[k].apply(console, args)
}
break
case 'count':
case 'time':
console[k] = (...args) => {
args[0] = `${args[0] || ''} {caller: ${getStackFactory()}}`
return functions[k].apply(console, args)
}
break
case 'assert':
console[k] = (...args) => {
args[1] = `${args[1] || ''} {caller: ${getStackFactory()}}`
return functions[k].apply(console, args)
}
break
}
})
return console
}
module.exports = consoleCaller