logger.js
3.42 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
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
var util = require('util')
var _ = require('lodash')
var loggerInstance
var defaultProvider = {
log: console.log,
debug: console.log, // use .log(); since console does not have .debug()
info: console.info,
warn: console.warn,
error: console.error
}
// log level 'weight'
var LEVELS = {
debug: 10,
info: 20,
warn: 30,
error: 50,
silent: 80
}
module.exports = {
// singleton
getInstance: function() {
if (!loggerInstance) {
loggerInstance = new Logger()
}
return loggerInstance
},
getArrow: getArrow
}
function Logger() {
var logLevel
var provider
var api = {
log: log,
debug: debug,
info: info,
warn: warn,
error: error,
setLevel: function(v) {
if (isValidLevel(v)) {
logLevel = v
}
},
setProvider: function(fn) {
if (fn && isValidProvider(fn)) {
provider = fn(defaultProvider)
}
}
}
init()
return api
function init() {
api.setLevel('info')
api.setProvider(function() {
return defaultProvider
})
}
// log will log messages, regardless of logLevels
function log() {
provider.log(_interpolate.apply(null, arguments))
}
function debug() {
if (_showLevel('debug')) {
provider.debug(_interpolate.apply(null, arguments))
}
}
function info() {
if (_showLevel('info')) {
provider.info(_interpolate.apply(null, arguments))
}
}
function warn() {
if (_showLevel('warn')) {
provider.warn(_interpolate.apply(null, arguments))
}
}
function error() {
if (_showLevel('error')) {
provider.error(_interpolate.apply(null, arguments))
}
}
/**
* Decide to log or not to log, based on the log levels 'weight'
* @param {String} showLevel [debug, info, warn, error, silent]
* @return {Boolean}
*/
function _showLevel(showLevel) {
var result = false
var currentLogLevel = LEVELS[logLevel]
if (currentLogLevel && currentLogLevel <= LEVELS[showLevel]) {
result = true
}
return result
}
// make sure logged messages and its data are return interpolated
// make it possible for additional log data, such date/time or custom prefix.
function _interpolate() {
var fn = _.spread(util.format)
var result = fn(_.slice(arguments))
return result
}
function isValidProvider(fnProvider) {
var result = true
if (fnProvider && !_.isFunction(fnProvider)) {
throw new Error('[HPM] Log provider config error. Expecting a function.')
}
return result
}
function isValidLevel(levelName) {
var validLevels = _.keys(LEVELS)
var isValid = _.includes(validLevels, levelName)
if (!isValid) {
throw new Error('[HPM] Log level error. Invalid logLevel.')
}
return isValid
}
}
/**
* -> normal proxy
* => router
* ~> pathRewrite
* ≈> router + pathRewrite
*
* @param {String} originalPath
* @param {String} newPath
* @param {String} originalTarget
* @param {String} newTarget
* @return {String}
*/
function getArrow(originalPath, newPath, originalTarget, newTarget) {
var arrow = ['>']
var isNewTarget = originalTarget !== newTarget // router
var isNewPath = originalPath !== newPath // pathRewrite
if (isNewPath && !isNewTarget) {
arrow.unshift('~')
} else if (!isNewPath && isNewTarget) {
arrow.unshift('=')
} else if (isNewPath && isNewTarget) {
arrow.unshift('≈')
} else {
arrow.unshift('-')
}
return arrow.join('')
}