forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
149 lines (136 loc) · 4.46 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
"use strict";
// Log class
var config = require('./config');
var plugins;
var connection;
var outbound;
var constants = require('./constants');
var util = require('util');
var logger = exports;
logger.LOGDATA = 9;
logger.LOGPROTOCOL = 8;
logger.LOGDEBUG = 7;
logger.LOGINFO = 6;
logger.LOGNOTICE = 5;
logger.LOGWARN = 4;
logger.LOGERROR = 3;
logger.LOGCRIT = 2;
logger.LOGALERT = 1;
logger.LOGEMERG = 0;
var loglevel = logger.LOGWARN;
var deferred_logs = [];
logger.dump_logs = function (exit) {
while (deferred_logs.length > 0) {
var log_item = deferred_logs.shift();
console.log(log_item.data);
}
if (exit) {
process.exit(1);
}
}
logger.log = function (level, data) {
if (level === 'PROTOCOL') {
data = data.replace(/\n/g, '\\n\n');
}
data = data.replace(/\r/g, '\\r')
.replace(/\n$/, '');
// todo - just buffer these up (defer) until plugins are loaded
if (plugins && plugins.plugin_list) {
while (deferred_logs.length > 0) {
var log_item = deferred_logs.shift();
plugins.run_hooks('log', logger, log_item);
}
plugins.run_hooks('log', logger, {
'level' : level,
'data' : data
});
}
else {
deferred_logs.push({
'level' : level,
'data' : data
});
}
}
logger.log_respond = function (retval, msg, data) {
// any other return code is irrelevant
if (retval === constants.cont) {
return console.log(data.data);
}
};
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 || loglevel_num === NaN) {
this.log('info', 'loglevel: ' + _loglevel.toUpperCase());
loglevel = logger[_loglevel.toUpperCase()];
}
else {
loglevel = loglevel_num;
}
if (!loglevel) {
loglevel = logger.LOGWARN;
}
}
};
logger.would_log = function (level) {
if (loglevel < level) return false;
return true;
}
logger._init_loglevel();
var level, key;
for (key in logger) {
if(logger.hasOwnProperty(key)) {
if (key.match(/^LOG\w/)) {
level = key.slice(3);
logger[key.toLowerCase()] = (function(level, key) {
return function() {
if (loglevel < logger[key])
return;
var levelstr = "[" + level + "]";
var str = "";
var uuidstr = "[-]";
var pluginstr = "[core]";
for (var i = 0; i < arguments.length; i++) {
var data = arguments[i];
if (typeof(data) === 'object') {
// if the object is a connection, we wish to 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 instanceof outbound.HMailItem) {
pluginstr = "[outbound]";
if (data.todo && data.todo.uuid) {
uuidstr = "[" + data.todo.uuid + "]";
}
}
else {
str += util.inspect(data);
}
}
else {
str += data;
}
}
logger.log(level, [levelstr, uuidstr, pluginstr, str].join(" "));
}
})(level, key);
}
}
}
// load these down here so it sees all the logger methods compiled above
plugins = require('./plugins');
connection = require('./connection');
outbound = require('./outbound');