-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (68 loc) · 2.22 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
'use strict'
const { inspect, format } = require('util')
const colors = [2, 3, 4, 5, 6]
let disabledAll = false
let instanceCounter = 0
function getColor() {
const c = (instanceCounter % colors.length)
instanceCounter++
return (c === 0) ? 2 : 2+c
}
//Support colors: white|grey|black|blue|cyan|green|magenta|red|yellow
function sub(namespace, color = '') {
color = (color === '') ? '3'+this.color : inspect.colors[color][0]
if (this.namespace) {
namespace = ':' + namespace
}
return create(`${this.namespace}\u001b[${color};1m${namespace}\u001b[0m`, this.color);
}
function error(...args) {
args[0] = `\u001b[31;1m${args[0]}\u001b[0m`
this(...args)
}
function success(...args) {
args[0] = `\u001b[32;1m${args[0]}\u001b[0m`
this(...args)
}
function warning(...args) {
args[0] = `\u001b[33;1m${args[0]}\u001b[0m`
this(...args)
}
function create(namespace = '', color) {
log.namespace = namespace
log.color = color ? color : getColor()
log.sub = sub
log.error = error
log.success = success
log.warning = warning
function log(...args) {
// if (process.env.NODE_ENV === 'production') {
// return
// }
const self = log
// Parse and reformat messages
if (typeof args[0] !== 'string') {
args.unshift('%s')
} else {
args[0] = args[0].replace(/%([oO])/g, '%s')
}
for(let i = 1; i < args.length; i++) {
switch(typeof args[i]) {
case 'function':
args[i] = args[i].toString()
break
case 'object':
args[i] = (args[i] instanceof Error) ? args[i].stack || args[i].message : inspect(args[i], false, null, true)
break
}
}
let prefix, str = format(...args)
if (self.namespace) {
prefix = `\u001b[3${self.color};1m${self.namespace} \u001b[0m`
str = `${prefix}` + str.split('\n').join('\n' + prefix)
}
process.stderr.write(str + '\n')
}
return log
}
module.exports = create