-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
172 lines (143 loc) · 3.03 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
/**
* Module dependencies.
*/
var stringify = require('json-stringify-safe');
var debug = require('debug')('logger');
var assert = require('assert');
var axon = require('axon');
var os = require('os');
var host = os.hostname();
/**
* Log level env var.
*/
var env = process.env.NODE_ENV || 'dev';
var test = 'test' == env;
var filter = process.env.LOG_LEVEL;
// filter defaults
filter = 'dev' == env
? (filter || 'debug')
: (filter || 'info');
/**
* Expose `Logger`.
*/
exports = module.exports = Logger;
/**
* Log levels.
*/
var levels = exports.levels = {
debug: 0,
info: 1,
warn: 2,
error: 3,
critical: 4,
alert: 5,
emergency: 6
};
/**
* Initialize a logger with the given `addrs`.
*
* @param {Array|String} addrs
* @param {Object} options
* @api public
*/
function Logger(addrs, options) {
if ('string' == typeof addrs) addrs = [addrs];
options = options || {};
this.filter = levels[filter];
assert(addrs, 'log-server addresses required');
this.stdio = true;
this.indent = 2; // 2 is the default legacy value.
// 0 is a valid value, so use hasOwnProperty.
if (options.hasOwnProperty('indent')) {
this.indent = options.indent;
}
this.sock = axon.socket('push');
this.sock.set('hwm', options.hwm || Infinity);
this.sock.format('json');
this.connect(addrs);
}
/**
* Connect to `addrs`.
*
* @param {Array} addrs
* @api private
*/
Logger.prototype.connect = function(addrs){
debug('connect to %j', addrs);
var sock = this.sock;
addrs.forEach(function(addr){
sock.connect(addr);
});
}
/**
* Disable stdio.
*
* @return {Logger}
* @api public
*/
Logger.prototype.silent = function(){
this.stdio = false;
return this;
};
/**
* Send log `msg` of `type` at `level`.
*
* @param {String} level
* @param {String} type
* @param {Object} msg
* @api public
*/
Logger.prototype.send = function(level, type, msg){
// filter
var n = levels[level];
if (n < this.filter) return;
// default msg
if (null == msg) msg = {};
// expose error messages
if (msg instanceof Error) msg = clone(msg);
if (msg.error instanceof Error) msg.error = clone(msg.error);
// timestamp
var now = new Date;
// stdio
if (this.stdio && !test) {
var meth = n > levels.info ? 'error' : 'log';
if ('dev' == env) {
console[meth]('%s - %s - %s', level, type, stringify(msg, null, this.indent));
} else {
console[meth]('%s (%s) - %s - %s', now.toUTCString(), level, type, stringify(msg, null, this.indent));
}
}
// send
this.sock.send({
timestamp: +now,
hostname: host,
message: msg,
level: level,
type: type
});
};
/**
* Log levels.
*/
Object.keys(levels).forEach(function(level){
Logger.prototype[level] = function(type, msg){
this.send(level, type, msg);
};
});
/**
* Clone `err` with enumerable `.message`.
*
* @param {Error} err
* @return {Object}
* @api private
*/
function clone(err){
var ret = {
message: err.message,
stack: err.stack
};
for (var key in err) {
ret[key] = err[key];
}
return ret;
}