forked from akaspin/ain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
233 lines (215 loc) · 5.75 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
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
232
233
var dgram = require('dgram');
var Buffer = require('buffer').Buffer;
var Facility = {
kern: 0,
user: 1,
mail: 2,
daemon: 3,
auth: 4,
syslog: 5,
lpr: 6,
news: 7,
uucp: 8,
local0: 16,
local1: 17,
local2: 18,
local3: 19,
local4: 20,
local5: 21,
local6: 22,
local7: 23
};
var Severity = {
emerg: 0,
alert: 1,
crit: 2,
err: 3,
warn: 4,
notice: 5,
info: 6,
debug: 7
};
// Format RegExp
var formatRegExp = /%[sdj]/g;
/**
* Just copy from node.js console
* @param f
* @returns
*/
function format(f) {
if (typeof f !== 'string') {
var objects = [], util = require('util');
for (var i = 0; i < arguments.length; i++) {
objects.push(util.inspect(arguments[i]));
}
return objects.join(' ');
}
var i = 1;
var args = arguments;
var str = String(f).replace(formatRegExp, function(x) {
switch (x) {
case '%s': return args[i++];
case '%d': return +args[i++];
case '%j': return JSON.stringify(args[i++]);
default:
return x;
}
});
for (var len = args.length; i < len; ++i) {
str += ' ' + args[i];
}
return str;
}
function leadZero(n) {
if (n < 10) {
return '0' + n;
} else {
return n;
}
}
/**
* Get current date in syslog format. Thanks https://github.com/kordless/lodge
* @returns {String}
*/
function getDate() {
var dt = new Date();
var hours = leadZero(dt.getHours());
var minutes = leadZero(dt.getMinutes());
var seconds = leadZero(dt.getSeconds());
var month = dt.getMonth();
var day = dt.getDate();
(day < 10) && (day = ' ' + day);
var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec' ];
return months[month] + " " + day + " " + hours + ":" + minutes
+ ":" + seconds;
}
/**
* Syslog logger
* @constructor
* @returns {SysLogger}
*/
function SysLogger() {
this._times = {};
}
/**
* Init function. All arguments is optional
* @param {String} tag By default is __filename
* @param {Facility|Number|String} By default is "user"
* @param {String} hostname By default is "localhost"
*/
SysLogger.prototype.set = function(tag, facility, hostname) {
this.setTag(tag);
this.setFacility(facility);
this.setHostname(hostname);
return this;
};
SysLogger.prototype.setTag = function(tag) {
this.tag = tag || __filename;
return this;
};
SysLogger.prototype.setFacility = function(facility) {
this.facility = facility || Facility.user;
if (typeof this.facility == 'string')
this.facility = Facility[this.facility];
return this;
};
SysLogger.prototype.setHostname = function(hostname) {
this.hostname = hostname || 'localhost';
return this;
};
/**
* Get new instance of SysLogger. All arguments is similar as `set`
* @returns {SysLogger}
*/
SysLogger.prototype.get = function() {
var newLogger = new SysLogger();
newLogger.set.apply(newLogger, arguments);
return newLogger;
};
/**
* Send message
* @param {String} message
* @param {Severity} severity
*/
SysLogger.prototype._send = function(message, severity) {
var client = dgram.createSocket('udp4');
var message = new Buffer('<' + (this.facility * 8 + severity) + '>' +
getDate() + ' ' + this.hostname + ' ' +
this.tag + '[' + process.pid + ']:' + message);
client.send(message, 0, message.length, 514, '127.0.0.1',
function(err) {
if (err) console.error('Can\'t connect to localhost:514');
});
client.close();
};
/**
* Send formatted message to syslog
* @param {String} message
* @param {Number|String} severity
*/
SysLogger.prototype.send = function(message, severity) {
severity = severity || Severity.notice;
if (typeof severity == 'string') severity = Severity[severity];
this._send(message, severity);
};
/**
* Send log message with notice severity.
*/
SysLogger.prototype.log = function() {
this._send(format.apply(this, arguments), Severity.notice);
};
/**
* Send log message with info severity.
*/
SysLogger.prototype.info = function() {
this._send(format.apply(this, arguments), Severity.info);
};
/**
* Send log message with warn severity.
*/
SysLogger.prototype.warn = function() {
this._send(format.apply(this, arguments), Severity.warn);
};
/**
* Send log message with err severity.
*/
SysLogger.prototype.error = function() {
this._send(format.apply(this, arguments), Severity.err);
};
/**
* Send log message with debug severity.
*/
SysLogger.prototype.debug = function() {
this._send(format.apply(this, arguments), Severity.debug);
};
/**
* Log object with `util.inspect` with notice severity
*/
SysLogger.prototype.dir = function(object) {
var util = require('util');
this._send(util.inspect(object) + '\n', Severity.notice);
};
SysLogger.prototype.time = function(label) {
this._times[label] = Date.now();
};
SysLogger.prototype.timeEnd = function(label) {
var duration = Date.now() - this._times[label];
this.log('%s: %dms', label, duration);
};
SysLogger.prototype.trace = function(label) {
var err = new Error;
err.name = 'Trace';
err.message = label || '';
Error.captureStackTrace(err, arguments.callee);
this.error(err.stack);
};
SysLogger.prototype.assert = function(expression) {
if (!expression) {
var arr = Array.prototype.slice.call(arguments, 1);
this._send(format.apply(this, arr), Severity.err);
}
};
var logger = new SysLogger();
logger.set();
module.exports = logger;