transport.js
6.83 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var assert = require('assert'),
winston = require('../../lib/winston'),
helpers = require('../helpers');
module.exports = function (transport, options) {
var logger = transport instanceof winston.Logger
? transport
: new winston.Logger({
transports: [
new transport(options)
]
});
// hack to fix transports that don't log
// any unit of time smaller than seconds
var common = require('../../lib/winston/common');
common.timestamp = function() {
return new Date().toISOString();
};
var transport = logger.transports[logger._names[0]];
var out = {
'topic': logger,
'when passed valid options': {
'should have the proper methods defined': function () {
switch (transport.name) {
case 'console':
helpers.assertConsole(transport);
break;
case 'file':
helpers.assertFile(transport);
break;
case 'couchdb':
helpers.assertCouchdb(transport);
break;
}
assert.isFunction(transport.log);
}
},
'the log() method': helpers.testNpmLevels(transport,
'should respond with true', function (ign, err, logged) {
assert.isNull(err);
assert.isNotNull(logged);
}
),
'the stream() method': {
'using no options': {
'topic': function () {
if (!transport.stream) return;
logger.log('info', 'hello world', {});
var cb = this.callback,
j = 10,
i = 10,
results = [],
stream = logger.stream();
stream.on('log', function (log) {
results.push(log);
results.stream = stream;
if (!--j) cb(null, results);
});
stream.on('error', function (err) {
j = -1; //don't call the callback again
cb(err);
});
while (i--) logger.log('info', 'hello world ' + i, {});
},
'should stream logs': function (err, results) {
if (!transport.stream) return;
assert.isNull(err);
results.forEach(function (log) {
assert.ok(log.message.indexOf('hello world') === 0
|| log.message.indexOf('test message') === 0);
});
results.stream.destroy();
}
},
'using the `start` option': {
'topic': function () {
if (!transport.stream) return;
var cb = this.callback,
stream = logger.stream({ start: 0 });
stream.on('log', function (log) {
log.stream = stream;
if (cb) cb(null, log);
cb = null;
});
},
'should stream logs': function (err, log) {
if (!transport.stream) return;
assert.isNull(err);
assert.isNotNull(log.message);
log.stream.destroy();
}
}
},
'after the logs have flushed': {
topic: function () {
setTimeout(this.callback, 1000);
},
'the query() method': {
'using basic querying': {
'topic': function () {
if (!transport.query) return;
var cb = this.callback;
logger.log('info', 'hello world', {}, function () {
logger.query(cb);
});
},
'should return matching results': function (err, results) {
if (!transport.query) return;
assert.isNull(err);
results = results[transport.name];
while (!Array.isArray(results)) {
results = results[Object.keys(results).pop()];
}
var log = results.pop();
assert.ok(log.message.indexOf('hello world') === 0
|| log.message.indexOf('test message') === 0);
}
},
'using the `rows` option': {
'topic': function () {
if (!transport.query) return;
var cb = this.callback;
logger.log('info', 'hello world', {}, function () {
logger.query({ rows: 1 }, cb);
});
},
'should return one result': function (err, results) {
if (!transport.query) return;
assert.isNull(err);
results = results[transport.name];
while (!Array.isArray(results)) {
results = results[Object.keys(results).pop()];
}
assert.equal(results.length, 1);
}
},
'using `fields` and `order` option': {
'topic': function () {
if (!transport.query) return;
var cb = this.callback;
logger.log('info', 'hello world', {}, function () {
logger.query({ order: 'asc', fields: ['timestamp'] }, cb);
});
},
'should return matching results': function (err, results) {
if (!transport.query) return;
assert.isNull(err);
results = results[transport.name];
while (!Array.isArray(results)) {
results = results[Object.keys(results).pop()];
}
assert.equal(Object.keys(results[0]).length, 1);
assert.ok(new Date(results.shift().timestamp)
< new Date(results.pop().timestamp));
}
},
'using the `from` and `until` option': {
'topic': function () {
if (!transport.query) return;
var cb = this.callback;
var start = Date.now() - (100 * 1000);
var end = Date.now() + (100 * 1000);
logger.query({ from: start, until: end }, cb);
},
'should return matching results': function (err, results) {
if (!transport.query) return;
assert.isNull(err);
results = results[transport.name];
while (!Array.isArray(results)) {
results = results[Object.keys(results).pop()];
}
assert.ok(results.length >= 1);
}
},
'using a bad `from` and `until` option': {
'topic': function () {
if (!transport.query) return;
var cb = this.callback;
logger.log('info', 'bad from and until', {}, function () {
var now = Date.now() + 1000000;
logger.query({ from: now, until: now }, cb);
});
},
'should return no results': function (err, results) {
if (!transport.query) return;
assert.isNull(err);
results = results[transport.name];
while (!Array.isArray(results)) {
results = results[Object.keys(results).pop()];
}
results = [results.filter(function(log) {
return log.message === 'bad from and until';
}).pop()];
assert.isUndefined(results[0]);
}
}
}
}
};
return out;
};