-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (49 loc) · 1.49 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
'use strict';
const log = require('fancy-log');
const colors = require('colors/safe');
const colorMap = {
info: 'blue',
dir: 'yellow',
warn: 'yellow',
error: 'red',
success: 'green',
lame: 'grey',
evil: 'zalgo',
};
const getLogger = ({ name = '', separator = '-' } = {}) => {
let enabled = true;
const decorate = (fn, level) => (...params) => {
if (enabled) {
let logParams = [];
if (name.length) {
logParams.push(name);
if (separator.length) {
logParams.push(separator);
}
}
logParams.push(...params);
if (colorMap[level]) {
const color = colorMap[level];
logParams = logParams.map(param => (typeof param === 'string' ? colors[color](param) : param));
}
return fn(...logParams);
}
return null;
};
return {
log: decorate(log),
success: decorate(log, 'success'),
evil: decorate(log, 'evil'),
lame: decorate(log, 'lame'),
...['info', 'dir', 'warn', 'error'].reduce(
(acc, level) => ({ ...acc, [level]: decorate(log[level], level) }),
{}
),
enable: () => (enabled = true),
disable: () => (enabled = false),
toggleEnabled: () => (enabled = !enabled),
setEnabled: e => (enabled = e),
};
};
module.exports = getLogger();
module.exports.getLogger = getLogger;