This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (106 loc) · 2.97 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import debug from 'debug'
const colors = {
red: {
terminal: 1,
rgb: '#ff0000',
},
green: {
terminal: 2,
rgb: '#00a000',
},
yellow: {
terminal: 3,
rgb: '#fcae05',
},
magenta: {
terminal: 4,
rgb: '#ad9aaf',
},
blue: {
terminal: 5,
rgb: '#004fd8',
},
gray: {
terminal: 7,
rgb: '#777777',
},
}
const levels = {
cfg: {
color: colors.blue,
suffix: 'CFG',
},
dbg: {
color: colors.magenta,
suffix: 'DBG',
},
log: {
color: colors.gray,
suffix: 'LOG',
},
info: {
color: colors.green,
suffix: 'INF',
},
warn: {
color: colors.yellow,
suffix: 'WRN',
},
error: {
color: colors.red,
suffix: 'ERR',
},
}
const timeStrGet = date => {
const msStr = date.getMilliseconds().toString().padStart(3, '0')
const dateTimeStr = date.toLocaleTimeString('en-GB', {
timeZone: 'UTC',
year: '2-digit',
month: '2-digit',
day: '2-digit',
hour12: false,
})
return `${dateTimeStr}.${msStr}`
}
const timestamp = function () {}
timestamp.toString = () => timeStrGet(new Date())
export const logger = {
create(nameSpace) {
const nsl = {
handlers: {},
}
for (const [levelName, levelInfo] of Object.entries(levels)) {
const {handlers} = nsl
Object.defineProperty(nsl, levelName, {
get() {
if (!nsl.handlers[levelName]) {
if (!nsl.instance) {
nsl.instance = debug(nameSpace)
// console.log(`lazyInit namespace ${nameSpace} useColors ${nsl.instance.useColors}`);
}
// console.log(`lazy init logger nameSpace ${nameSpace}:${levelInfo.suffix}`);
const handler = nsl.instance.extend(levelInfo.suffix)
// devtools timestamps can be enabled in settings
handler.log = window ? console.log.bind(console) : console.log.bind(console, '%s', timestamp)
const colorKey = window ? 'rgb' : 'terminal'
handler.color = levelInfo.color[colorKey]
handler.useColors = true
handlers[levelName] = handler
}
return handlers[levelName]
},
})
}
return nsl
},
init(namespacesList) {
const namespaces = namespacesList.join(',')
// console.log('logged namespaces:', namespaces);
debug.enable(namespaces)
// {
// const testLogger = logger.create('test-logger');
// ['cfg', 'dbg', 'log', 'info', 'warn', 'error',]
// .forEach(level => testLogger[level](`test level ${level} message`));
// }
},
}