logger.js
2.54 KB
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
import moment from "moment";
export default {
log: (...args) => {
const req = args[0];
if (typeof req === 'object' && req.hasOwnProperty("specialType")) {
args[0] = req.headers.__reqId;
}
console.log(`\x1b[0m${getTime()} INFO ${toString(args).join('')}\x1b[0m`)
},
info: (...args) => {
const req = args[0];
if (typeof req === 'object' && req.hasOwnProperty("specialType")) {
args[0] = req.headers.__reqId;
}
console.log(`\x1b[36m${getTime()} INFO ${toString(args).join('')}\x1b[0m`)
},
error: (...args) => {
const req = args[0];
if (typeof req === 'object' && req.hasOwnProperty("specialType")) {
args[0] = req.headers.__reqId;
}
console.log(`\x1b[31m${getTime()} ERROR ${toString(args).join('')}\x1b[0m`)
},
warning: (...args) => {
const req = args[0];
if (typeof req === 'object' && req.hasOwnProperty("specialType")) {
args[0] = req.headers.__reqId;
}
console.log(`\x1b[33m${getTime()} INFO ${toString(args).join('')}\x1b[0m`)
},
success: (...args) => {
const req = args[0];
if (typeof req === 'object' && req.hasOwnProperty("specialType")) {
args[0] = req.headers.__reqId;
}
console.log(`\x1b[32m${getTime()} INFO ${toString(args).join('')}\x1b[0m`)
},
magenta: (...args) => {
const req = args[0];
if (typeof req === 'object' && req.hasOwnProperty("specialType")) {
args[0] = req.headers.__reqId;
}
console.log(`\x1b[35m${getTime()} INFO ${toString(args).join('')}\x1b[0m`)
},
random: (...args) => {
const req = args[0];
if (typeof req === 'object' && req.hasOwnProperty("specialType")) {
args[0] = req.headers.__reqId;
}
console.log((toString(args).join('').split('').map(item => '\x1b[' + randomColor() + 'm' + item + '\x1b[0m')).join('') + '\n')
},
db: (...args) => {
const req = args[0];
if (typeof req === 'object' && req.hasOwnProperty("specialType")) {
args[0] = req.headers.__reqId;
}
console.log(`\x1b[35m${getTime()} INFO [Database Message] ${toString(args).join('')}\x1b[0m`)
},
}
/**
* Random color not duplicate next character
*/
let tempColor = null;
let randomColor = () => {
if (tempColor != null) {
const rand = Math.floor(Math.random() * (37 - 31) + 31);
if (rand != tempColor) {
tempColor = rand;
return rand;
} else {
return randomColor();
}
} else {
tempColor = Math.floor(Math.random() * (37 - 31) + 31);
return tempColor;
}
}
let toString = (list) => {
return list.map(item => item.constructor.name == 'Object' || item.constructor.name == 'Array' ? JSON.stringify(item) : item);
}
let getTime = () => moment().format("YYYY-MM-DD HH:mm:ss:SSS");