-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.js
174 lines (133 loc) · 3.14 KB
/
print.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict'
const _colors = require('./utils/colors')
const pad = require('./utils/string').pad
class Print {
constructor(options) {
Object.assign(this, _colors)
this.levels = {}
this.plugins = {}
Print.defaultLevels( this )
Print.defaultPlugins( this )
// Defaults
this.use_tag = true
this.auto_trim = true
if (typeof options === 'object') {
Object.assign(this, options)
}
}
new(options) {
return new Print(options)
}
level(level, options) {
this.levels[level] = Object.assign({
visible: true
}, options || {})
this[level] = function() {
let str = [...arguments].join(' ')
// Test log level visibility
if (!this.levels[level].visible) return
// Apply options
str = this.applyPlugins( this.levels[level], str )
this._log( str )
}
}
plugin(name, fn, useByDefault) {
this.plugins[name] = fn.bind(this)
this['use_'+name] = typeof useByDefault === 'boolean' ? useByDefault : true
}
applyPlugins(level, str) {
for (const key in level) {
if (this.plugins[key] && this['use_' + key]) {
str = this.plugins[key](str, level[key])
}
}
return str
}
/**
*
* @param string
* @returns {string}
*/
clean(value) {
if (value === undefined) return
return value.toString().replace(/^(\s|\n)+|(\s|\n)+$/g, '')
}
visibility(level, visibility) {
if (this.levels[level]) {
this.levels[level].visible = visibility
}
}
silent() {
for (const key in this.levels) {
this.visibility(key, false)
}
}
verbose() {
for (const key in this.levels) {
this.visibility(key, true)
}
}
defaults() {
this.visibility('log' , true)
this.visibility('debug' , false)
this.visibility('error' , true)
this.visibility('warn' , false)
}
_log() {
const args = arguments
if (this.auto_trim) {
for ( let i = 0, len = args.length; i < len; i++ ) {
if (typeof args[i] === 'string' && args[i].trim)
args[i] = args[i].trim()
}
}
console.log.apply(null, args)
}
}
/**
* Setup default log levels
*/
Print.defaultLevels = function(PrintObject) {
PrintObject.level('log')
PrintObject.level('debug', {
style: 'grey',
date: true
})
PrintObject.level('warn', {
style: 'yellow',
tag: {
tag: '?!',
style: 'yellow'
},
date: true
})
PrintObject.level('error', {
style: 'red',
tag: {
tag: '!!',
style: 'red'
},
date: true
})
}
/**
* Setup default plugins
*/
Print.defaultPlugins = function(PrintObject) {
PrintObject.plugin('style', function(str, style) {
return this[style]( str )
})
PrintObject.plugin('tag', function(str, o) {
const tag = this[o.style](`[${o.tag}]`)
return `${tag} ${str}`
}, false)
PrintObject.plugin('date', function(str) {
const d = new Date();
const h = pad(d.getHours(), 2, '0')
const m = pad(d.getMinutes(), 2, '0')
const s = pad(d.getSeconds(), 2, '0')
const date = this.cyan(`[${h}:${m}:${s}]`)
return `${date} ${str}`
})
}
module.exports = new Print