forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
231 lines (200 loc) · 6.2 KB
/
logger.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'use strict';
// Log class
var constants = require('haraka-constants');
var config = require('./config');
var plugins;
var connection;
var outbound;
var util = require('util');
var tty = require('tty');
var logger = exports;
logger.levels = {
DATA: 9,
PROTOCOL: 8,
DEBUG: 7,
INFO: 6,
NOTICE: 5,
WARN: 4,
ERROR: 3,
CRIT: 2,
ALERT: 1,
EMERG: 0,
};
for (var le in logger.levels) {
logger['LOG' + le] = logger.levels[le];
}
logger.loglevel = logger.LOGWARN;
logger.deferred_logs = [];
logger.colors = {
"DATA" : "green",
"PROTOCOL" : "green",
"DEBUG" : "grey",
"INFO" : "cyan",
"NOTICE" : "blue",
"WARN" : "red",
"ERROR" : "red",
"CRIT" : "red",
"ALERT" : "red",
"EMERG" : "red",
};
var stdout_is_tty = tty.isatty(process.stdout.fd);
logger.colorize = function (color, str) {
if (!util.inspect.colors[color]) { return str; } // unknown color
return '\u001b[' + util.inspect.colors[color][0] + 'm' + str +
'\u001b[' + util.inspect.colors[color][1] + 'm';
};
logger.dump_logs = function (cb) {
while (logger.deferred_logs.length > 0) {
var log_item = logger.deferred_logs.shift();
plugins.run_hooks('log', logger, log_item);
}
// Run callback after flush
if (cb) process.stdout.write('', cb);
return true;
};
if (!util.isFunction) {
util.isFunction = function (functionToCheck) {
var getType = {};
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
};
}
logger.dump_and_exit = function (code) {
this.dump_logs(function () {
if (util.isFunction(code)) return code();
process.exit(code);
});
}
logger.log = function (level, data) {
if (level === 'PROTOCOL') {
data = data.replace(/\n/g, '\\n');
}
data = data.replace(/\r/g, '\\r')
.replace(/\n$/, '');
var item = { 'level' : level, 'data' : data };
// buffer until plugins are loaded
if (!plugins || (Array.isArray(plugins.plugin_list) &&
!plugins.plugin_list.length))
{
logger.deferred_logs.push(item);
return true;
}
// process buffered logs
while (logger.deferred_logs.length > 0) {
var log_item = logger.deferred_logs.shift();
plugins.run_hooks('log', logger, log_item);
}
plugins.run_hooks('log', logger, item );
return true;
};
logger.log_respond = function (retval, msg, data) {
// any other return code is irrelevant
if (retval !== constants.cont) { return false; }
var color = logger.colors[data.level];
if (color && stdout_is_tty) {
console.log(logger.colorize(color,data.data));
return true;
}
console.log(data.data);
return true;
};
logger._init_loglevel = function () {
var self = this;
var _loglevel = config.get('loglevel', 'value', function () {
self._init_loglevel();
});
if (_loglevel) {
var loglevel_num = parseInt(_loglevel);
if (!loglevel_num || isNaN(loglevel_num)) {
this.log('INFO', 'loglevel: ' + _loglevel.toUpperCase());
logger.loglevel = logger[_loglevel.toUpperCase()];
}
else {
logger.loglevel = loglevel_num;
}
if (!logger.loglevel) {
this.log('WARN', 'invalid loglevel: ' + _loglevel + ' defaulting to LOGWARN');
logger.loglevel = logger.LOGWARN;
}
}
};
logger.would_log = function (level) {
if (logger.loglevel < level) { return false; }
return true;
};
var original_console_log = console.log;
logger._init_timestamps = function () {
var self = this;
var _timestamps = config.get('log_timestamps', 'value', function () {
self._init_timestamps();
});
if (_timestamps) {
console.log = function () {
var new_arguments = [new Date().toISOString()];
for (var key in arguments) {
new_arguments.push(arguments[key]);
}
original_console_log.apply(console, new_arguments);
};
}
else {
console.log = original_console_log;
}
};
logger._init_loglevel();
logger._init_timestamps();
logger.log_if_level = function (level, key, plugin) {
return function () {
if (logger.loglevel < logger[key]) { return; }
var levelstr = '[' + level + ']';
var str = '';
var uuidstr = '[-]';
var pluginstr = '[' + (plugin || 'core') + ']';
for (var i=0; i < arguments.length; i++) {
var data = arguments[i];
if (typeof data !== 'object') {
str += data;
continue;
}
if (!data) continue;
// if the object is a connection, add the connection id
if (data instanceof connection.Connection) {
uuidstr = '[' + data.uuid;
if (data.tran_count > 0) {
uuidstr += "." + data.tran_count;
}
uuidstr += ']';
}
else if (data instanceof plugins.Plugin) {
pluginstr = '[' + data.name + ']';
}
else if (data.name) {
pluginstr = '[' + data.name + ']';
}
else if (data instanceof outbound.HMailItem) {
pluginstr = '[outbound]';
if (data.todo && data.todo.uuid) {
uuidstr = '[' + data.todo.uuid + ']';
}
}
else {
str += util.inspect(data);
}
}
logger.log(level, [levelstr, uuidstr, pluginstr, str].join(' '));
return true;
};
};
logger.add_log_methods = function (object, plugin) {
if (!object) return;
if (typeof(object) !== 'object') return;
for (var level in logger.levels) {
var fname = 'log' + level.toLowerCase();
if (object[fname]) continue; // already added
object[fname] = logger.log_if_level(level, 'LOG'+level, plugin);
}
};
logger.add_log_methods(logger);
// load these down here so it sees all the logger methods compiled above
plugins = require('./plugins');
connection = require('./connection');
outbound = require('./outbound');