logger.js
5.33 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var util = require('util');
var Settings = {
log_timestamp : false,
colors : true,
enabled : true
};
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'];
function getDate() {
var now = new Date();
return [now.toLocaleDateString(), now.toLocaleTimeString()].join(' ');
}
function pad(n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
}
// 26 Feb 16:19:34
function timestamp() {
var d = new Date();
var time = [pad(d.getHours()),
pad(d.getMinutes()),
pad(d.getSeconds())].join(':');
return [d.getDate(), months[d.getMonth()], time].join(' ');
}
function getPrefix(tag, severity) {
var levels = ['EMERG', 'ALERT', 'CRITICAL', 'ERROR', 'WARNING', 'NOTICE', 'INFO', 'DEBUG'];
return tag + ' ' + levels[severity] +' - ';
}
function ConsoleColor() {
var self = this;
var mappings = {
blue : '0;34',
light_blue : '1;34'
};
this.background = new Background();
this.foreground_colors = {};
this.foreground_colors['black'] = '0;30';
this.foreground_colors['dark_gray'] = '1;30';
this.foreground_colors['blue'] = '0;34';
this.foreground_colors['light_blue'] = '1;34';
this.foreground_colors['green'] = '0;32';
this.foreground_colors['light_green'] = '1;32';
this.foreground_colors['cyan'] = '0;36';
this.foreground_colors['light_cyan'] = '1;36';
this.foreground_colors['red'] = '0;31';
this.foreground_colors['light_red'] = '1;31';
this.foreground_colors['purple'] = '0;35';
this.foreground_colors['light_purple'] = '1;35';
this.foreground_colors['brown'] = '0;33';
this.foreground_colors['yellow'] = '1;33';
this.foreground_colors['light_gray'] = '0;37';
this.foreground_colors['white'] = '1;37';
this.foreground_colors['stack_trace'] = '0;90';
this.background_colors = {};
this.background_colors['black'] = '40';
this.background_colors['red'] = '41';
this.background_colors['green'] = '42';
this.background_colors['yellow'] = '43';
this.background_colors['blue'] = '44';
this.background_colors['magenta'] = '45';
this.background_colors['cyan'] = '46';
this.background_colors['light_gray'] = '47';
Object.keys(this.foreground_colors).forEach(function(k) {
ConsoleColor.prototype[k.toLowerCase()] = function Foreground(text, background) {
var string = '\033[' + self.foreground_colors[k.toLowerCase()] + 'm';
if (background !== undefined) {
string += background();
}
string += text + '\033[0m';
return string;
};
});
Object.keys(this.background_colors).forEach(function(k) {
Background.prototype[k.toLowerCase()] = function Background(text) {
return '\033[' + self.background_colors[k.toLowerCase()] + 'm';
};
});
return this;
}
function Background() { return this; }
var colors = new ConsoleColor();
function logObject(obj) {
console.log(util.inspect(obj, {
showHidden : false,
depth : 3,
colors : Settings.colors
}));
}
function logTimestamp() {
if (Settings.log_timestamp) {
return colors.white(timestamp()) + ' ';
}
return '';
}
function logMessage(type, message, args) {
if (!message || !Settings.enabled) {
return;
}
var messageStr = '';
var timestamp = logTimestamp();
switch (type) {
case 'ERROR':
messageStr = colors.yellow(type, colors.background.dark_gray) +' '+
timestamp + colors.light_green(message);
break;
case 'INFO':
messageStr = colors.light_purple(type, colors.background.black) +' '+
timestamp + colors.light_cyan(message);
break;
case 'LOG':
messageStr = colors.white(type+' ', colors.background.black) +' '+
timestamp + colors.white(message);
break;
case 'WARN':
messageStr = colors.light_green(type, colors.background.black) +' '+
timestamp + colors.light_green(message);
break;
}
process.stdout.write(messageStr);
if (args.length > 0) {
var inlineArgs = [];
args.forEach(function(item) {
if (Object.prototype.toString.call(item) === '[object Object]' && Object.keys(item).length > 0) {
if (inlineArgs.length) {
console.log.apply(console, inlineArgs);
inlineArgs = [];
}
logObject(item);
} else {
inlineArgs.push(item);
}
});
if (inlineArgs.length) {
process.stdout.write(' ');
console.log.apply(console, inlineArgs);
inlineArgs = [];
}
} else {
process.stdout.write('\n');
}
}
exports.info = function(message) {
var args = Array.prototype.slice.call(arguments, 1);
logMessage('INFO', message, args);
};
exports.log = function(message) {
var args = Array.prototype.slice.call(arguments, 1);
logMessage('LOG', message, args);
};
exports.warn = function(message) {
var args = Array.prototype.slice.call(arguments, 1);
logMessage('WARN', message, args);
};
exports.error = function(message) {
var args = Array.prototype.slice.call(arguments, 1);
logMessage('ERROR', message, args);
};
exports.disableColors = function () {
Settings.colors = false;
Object.keys(ConsoleColor.prototype).forEach(function (color) {
ConsoleColor.prototype[color] = function (text) {
return text;
};
});
};
exports.disable = function() {
Settings.enabled = false;
};
exports.enable = function() {
Settings.enabled = true;
};
exports.isEnabled = function() {
return Settings.enabled;
};
exports.colors = colors;