-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
172 lines (154 loc) · 5.05 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
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
var util = require('util'),
chalk = require('chalk'),
moment = require('moment'),
inherit = require('inherit'),
loggers = {},
Logger;
module.exports = Logger = inherit({
_STYLES: {
verbose: ['magenta'],
debug: ['cyan'],
info: ['green'],
warn: ['bold', 'yellow'],
error: ['bold', 'red']
},
_logger: undefined,
_prefixString: undefined,
_styleString: undefined,
_stub: function () {
return false;
},
/**
* Logger initialization function
* @param {Module} moduleForLog that uses this logger instance
* @private
*/
__constructor: function (moduleForLog) {
if (this.__self.options.mode === 'testing') {
this._logger = {
verbose: this._stub,
debug: this._stub,
info: this._stub,
warn: this._stub,
error: this._stub
};
} else {
var _this = this;
this._logger = ['verbose', 'debug', 'info', 'warn', 'error'].reduce(function (prev, item, index, arr) {
prev[item] = arr.slice(0, index + 1).indexOf(_this.__self.options.level) > -1 ?
(console[item] || console.log) : _this._stub;
return prev;
}, {});
}
this._styleString = function (s, styles) {
if (!this.__self.options.color) {
return s;
}
var f = styles.reduce(function (prev, item) {
prev = prev[item];
return prev;
}, chalk);
return f(s);
};
this._prefixString = function (level) {
var prefix = '';
if (this.__self.options.useDate) {
prefix = util.format('[%s] ', moment().format('YYYY-MM-DD HH:mm:ss'));
}
prefix += util.format('%s ', level.toUpperCase());
prefix = this._styleString(prefix, this._STYLES[level]);
prefix += moduleForLog['filename'].split('/').slice(-2).join('/') + ': ';
return prefix;
};
},
/**
* Logs given message args with given log level
* @param {String} level - log level
* @param {Argument} args - logger arguments
* @returns {Logger}
* @private
*/
_log: function (level, args) {
this._logger[level](this._prefixString(level) + util.format.apply(this, args));
return this;
},
/**
* Alias for logging verbose messages
* @returns {exports}
*/
verbose: function () {
return this._log('verbose', arguments);
},
/**
* Alias for logging debug messages
* @returns {exports}
*/
debug: function () {
return this._log('debug', arguments);
},
/**
* Alias for logging info messages
* @returns {exports}
*/
info: function () {
return this._log('info', arguments);
},
/**
* Alias for logging warn messages
* @returns {exports}
*/
warn: function () {
return this._log('warn', arguments);
},
/**
* Alias for logging error messages
* @returns {exports}
*/
error: function () {
return this._log('error', arguments);
}
}, {
options: null,
/**
* Clears loggers hash. Needs for tests
* @private
*/
_clearLoggers: function () {
loggers = {};
},
/**
* Sets custom options for logger
* @param {Object} options for logger
* @returns {Logger}
*/
setOptions: function (options) {
options = options || {},
this.options = this.options || {};
this.options.mode = options.mode || process.env['NODE_ENV'] || 'development';
this.options.level = options.level || 'info';
this.options.color = options.color !== false;
this.options.useDate = options.useDate !== false;
return this;
},
/**
* Drops all logger setting to default values
* @returns {Logger}
*/
resetOptions: function () {
return this.setOptions(null);
},
/**
* Creates logger for given module
* @param {Module} m - module for log. If it is omitted then logger own module will be used
* @returns {*}
*/
createLogger: function (m) {
if (!this.options) {
this.resetOptions();
}
m = m || module;
var fName = m['filename'];
loggers[fName] = loggers[fName] || new Logger(m);
return loggers[fName];
}
});