logger-test.js
13.3 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* logger-test.js: Tests for instances of the winston Logger
*
* (C) 2010 Charlie Robbins
* MIT LICENSE
*
*/
var path = require('path'),
vows = require('vows'),
assert = require('assert'),
util = require('util'),
winston = require('../lib/winston'),
helpers = require('./helpers'),
transport = require('./transports/transport');
vows.describe('winton/logger').addBatch({
"An instance of winston.Logger": {
"with transports": {
topic: new (winston.Logger)({ transports: [new (winston.transports.Console)({ level: 'info' })] }),
"should have the correct methods / properties defined": function (logger) {
helpers.assertLogger(logger);
},
"the add() with an unsupported transport": {
"should throw an error": function () {
assert.throws(function () { logger.add('unsupported') }, Error);
}
}
},
"with no transports": {
topic: new winston.Logger(),
"the log method": {
topic: function (logger) {
var that = this;
logger.log('error', 'This should be an error', function (err) {
that.callback(null, err);
});
},
"should respond with the appropriate error": function (err) {
assert.instanceOf(err, Error);
}
}
}
}
}).addBatch({
"An instance of winston.Logger": {
topic: new (winston.Logger)({ transports: [new (winston.transports.Console)({ level: 'info' })] }),
"the log() method": {
"when listening for the 'logging' event": {
topic: function (logger) {
logger.once('logging', this.callback);
logger.log('info', 'test message');
},
"should emit the 'log' event with the appropriate transport": function (transport, ign) {
helpers.assertConsole(transport);
}
},
"when listening for the 'logged' event": {
topic: function (logger) {
logger.once('logged', this.callback);
logger.log('info', 'test message');
},
"should emit the 'logged' event": function (level, msg, meta) {
assert.equal(level, 'info');
assert.equal(msg, 'test message');
}
},
}
}
}).addBatch({
"An instance of winston.Logger with no transports": {
topic: new (winston.Logger)({ emitErrs: true }),
"the log() method should throw an error": function (logger) {
assert.throws(function () { logger.log('anything') }, Error);
},
"the extend() method called on an empty object": {
topic: function (logger) {
var empty = {};
logger.extend(empty);
return empty;
},
"should define the appropriate methods": function (extended) {
['log', 'profile', 'startTimer'].concat(Object.keys(winston.config.npm.levels)).forEach(function (method) {
assert.isFunction(extended[method]);
});
}
},
"the add() method with a supported transport": {
topic: function (logger) {
return logger.add(winston.transports.Console);
},
"should add the console Transport onto transports": function (logger) {
assert.equal(helpers.size(logger.transports), 1);
helpers.assertConsole(logger.transports.console);
},
"should throw an error when the same Transport is added": function (logger) {
assert.throws(function () { logger.add(winston.transports.Console) }, Error);
},
"the profile() method": {
"when passed a callback": {
topic: function (logger) {
var cb = this.callback;
logger.profile('test1');
setTimeout(function () {
logger.profile('test1', function (err, level, msg, meta) {
cb(err, level, msg, meta, logger);
});
}, 50);
},
"should respond with the appropriate profile message": function (err, level, msg, meta, logger) {
assert.isNull(err);
assert.equal(level, 'info');
assert.match(meta.duration, /(\d+)ms/);
assert.isTrue(typeof logger.profilers['test'] === 'undefined');
},
"when passed some metadata": {
topic: function () {
var logger = arguments[arguments.length - 1];
var cb = this.callback.bind(null, null);
logger.profile('test3');
setTimeout(function () {
logger.once('logging', cb);
logger.profile('test3', {
some: 'data'
});
}, 50);
},
"should respond with the right metadata": function (err, transport, level, msg, meta) {
assert.equal(msg, 'test3');
assert.isNull(err);
assert.equal(level, 'info');
assert.match(meta.duration, /(\d+)ms/);
assert.equal(meta.some, 'data');
},
"when not passed a callback": {
topic: function () {
var logger = arguments[arguments.length - 1];
var cb = this.callback.bind(null, null);
logger.profile('test2');
setTimeout(function () {
logger.once('logging', cb);
logger.profile('test2');
}, 50);
},
"should respond with the appropriate profile message": function (err, transport, level, msg, meta) {
assert.isNull(err);
assert.equal(msg, 'test2');
assert.equal(level, 'info');
assert.match(meta.duration, /(\d+)ms/);
}
}
}
}
},
"the startTimer() method": {
"when passed a callback": {
topic: function (logger) {
var that = this;
var timer = logger.startTimer()
setTimeout(function () {
timer.done('test', function (err, level, msg, meta) {
that.callback(err, level, msg, meta, logger);
});
}, 500);
},
"should respond with the appropriate message": function (err, level, msg, meta, logger) {
assert.isNull(err);
assert.equal(level, 'info');
assert.match(meta.duration, /(\d+)ms/);
}
},
"when not passed a callback": {
topic: function (logger) {
var that = this;
var timer = logger.startTimer()
logger.once('logging', that.callback.bind(null, null));
setTimeout(function () {
timer.done();
}, 500);
},
"should respond with the appropriate message": function (err, transport, level, msg, meta) {
assert.isNull(err);
assert.equal(level, 'info');
assert.match(meta.duration, /(\d+)ms/);
var duration = parseInt(meta.duration);
assert.isNumber(duration);
assert.isTrue(duration >= 50 && duration < 100);
}
}
},
"and adding an additional transport": {
topic: function (logger) {
return logger.add(winston.transports.File, {
filename: path.join(__dirname, 'fixtures', 'logs', 'testfile2.log')
});
},
"should be able to add multiple transports": function (logger) {
assert.equal(helpers.size(logger.transports), 2);
helpers.assertConsole(logger.transports.console);
helpers.assertFile(logger.transports.file);
}
}
}
}
}).addBatch({
"The winston logger": {
topic: new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: path.join(__dirname, 'fixtures', 'logs', 'filelog.log' )})
]
}),
"should return have two transports": function (logger) {
assert.equal(helpers.size(logger.transports), 2);
},
"the remove() with an unadded transport": {
"should throw an Error": function (logger) {
assert.throws(function () { logger.remove(winston.transports.Webhook) }, Error);
}
},
"the remove() method with an added transport": {
topic: function (logger) {
return logger.remove(winston.transports.Console);
},
"should remove the Console transport from transports": function (logger) {
assert.equal(helpers.size(logger.transports), 1);
helpers.assertFile(logger.transports.file);
},
"and removing an additional transport": {
topic: function (logger) {
return logger.remove(winston.transports.File);
},
"should remove File transport from transports": function (logger) {
assert.equal(helpers.size(logger.transports), 0);
}
}
}
}
}).addBatch({
"The winston logger": {
topic: new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: path.join(__dirname, 'fixtures', 'logs', 'filelog.log' )})
]
}),
"the clear() method": {
"should remove all transports": function (logger) {
logger.clear();
assert.equal(helpers.size(logger.transports), 0);
}
}
}
}).addBatch({
"The winston logger": {
topic: new (winston.Logger)({
exceptionHandlers: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: path.join(__dirname, 'fixtures', 'logs', 'filelog.log' )})
]
}),
"the unhandleExceptions() method": {
"should remove all transports": function (logger) {
assert.equal(helpers.size(logger.exceptionHandlers), 2);
logger.unhandleExceptions();
assert.equal(helpers.size(logger.exceptionHandlers), 0);
}
}
}
}).addBatch({
"The winston logger": {
topic: new (winston.Logger)({
transports: [
new (winston.transports.Console)()
]
}),
"the log() method": {
"when passed a string placeholder": {
topic: function (logger) {
logger.once('logging', this.callback);
logger.log('info', 'test message %s', 'my string');
},
"should interpolate": function (transport, level, msg, meta) {
assert.strictEqual(msg, 'test message my string');
},
},
"when passed a number placeholder": {
topic: function (logger) {
logger.once('logging', this.callback);
logger.log('info', 'test message %d', 123);
},
"should interpolate": function (transport, level, msg, meta) {
assert.strictEqual(msg, 'test message 123');
},
},
"when passed a json placholder and an empty object": {
topic: function (logger) {
logger.once('logging', this.callback);
logger.log('info', 'test message %j', {number: 123}, {});
},
"should interpolate": function (transport, level, msg, meta) {
assert.strictEqual(msg, 'test message {"number":123}');
},
},
"when passed a escaped percent sign": {
topic: function (logger) {
logger.once('logging', this.callback);
logger.log('info', 'test message %%', {number: 123});
},
"should not interpolate": function (transport, level, msg, meta) {
assert.strictEqual(msg, util.format('test message %%'));
assert.deepEqual(meta, {number: 123});
},
},
"when passed interpolation strings and a meta object": {
topic: function (logger) {
logger.once('logging', this.callback);
logger.log('info', 'test message %s, %s', 'first', 'second' ,{number: 123});
},
"should interpolate and have a meta object": function (transport, level, msg, meta) {
assert.strictEqual(msg, 'test message first, second');
assert.deepEqual(meta, {number: 123});
},
},
"when passed multiple strings and a meta object": {
topic: function (logger) {
logger.once('logging', this.callback);
logger.log('info', 'test message', 'first', 'second' , {number: 123});
},
"should join and have a meta object": function (transport, level, msg, meta) {
assert.strictEqual(msg, 'test message first second');
assert.deepEqual(meta, {number: 123});
},
},
"when passed interpolations strings, meta object and a callback": {
topic: function (logger) {
var that = this;
logger.log('info', 'test message %s, %s', 'first', 'second' , {number: 123}, function(transport, level, msg, meta){
that.callback(transport, level, msg, meta)
});
},
"should interpolate and have a meta object": function (transport, level, msg, meta) {
assert.strictEqual(msg, 'test message first, second');
assert.deepEqual(meta, {number: 123});
},
},
"when passed multiple strings, a meta object and a callback": {
topic: function (logger) {
var that = this;
logger.log('info', 'test message', 'first', 'second' , {number: 123}, function(transport, level, msg, meta){
that.callback(transport, level, msg, meta)
});
},
"should join and have a meta object": function (transport, level, msg, meta) {
assert.strictEqual(msg, 'test message first second');
assert.deepEqual(meta, {number: 123});
},
}
}
}
}).export(module);