UNPKG

6.99 kBJavaScriptView Raw
1var assert = require('assert'),
2 winston = require('../../lib/winston'),
3 helpers = require('../helpers');
4
5module.exports = function (transport, options) {
6 var logger = transport instanceof winston.Logger
7 ? transport
8 : new winston.Logger({
9 transports: [
10 new transport(options)
11 ]
12 });
13
14 // hack to fix transports that don't log
15 // any unit of time smaller than seconds
16 var common = require('../../lib/winston/common');
17 common.timestamp = function() {
18 return new Date().toISOString();
19 };
20
21 var transport = logger.transports[logger._names[0]];
22
23 var out = {
24 'topic': logger,
25 'when passed valid options': {
26 'should have the proper methods defined': function () {
27 switch (transport.name) {
28 case 'console':
29 helpers.assertConsole(transport);
30 break;
31 case 'file':
32 helpers.assertFile(transport);
33 break;
34 case 'couchdb':
35 helpers.assertCouchdb(transport);
36 break;
37 }
38 assert.isFunction(transport.log);
39 }
40 },
41 'the log() method': helpers.testNpmLevels(transport,
42 'should respond with true', function (ign, err, logged) {
43 assert.isNull(err);
44 assert.isNotNull(logged);
45 }
46 ),
47 'the stream() method': {
48 'using no options': {
49 'topic': function () {
50 if (!transport.stream) return;
51
52 logger.log('info', 'hello world', {});
53
54 var cb = this.callback,
55 j = 10,
56 i = 10,
57 results = [],
58 stream = logger.stream();
59
60 stream.on('log', function (log) {
61 results.push(log);
62 results.stream = stream;
63 if (!--j) cb(null, results);
64 });
65
66 stream.on('error', function (err) {
67 j = -1; //don't call the callback again
68 cb(err);
69 });
70
71 while (i--) logger.log('info', 'hello world ' + i, {});
72 },
73 'should stream logs': function (err, results) {
74 if (!transport.stream) return;
75 assert.isNull(err);
76 results.forEach(function (log) {
77 assert.ok(log.message.indexOf('hello world') === 0
78 || log.message.indexOf('test message') === 0);
79 });
80 results.stream.destroy();
81 }
82 },
83 'using the `start` option': {
84 'topic': function () {
85 if (!transport.stream) return;
86
87 var cb = this.callback,
88 stream = logger.stream({ start: 0 });
89
90 stream.on('log', function (log) {
91 log.stream = stream;
92 if (cb) cb(null, log);
93 cb = null;
94 });
95 },
96 'should stream logs': function (err, log) {
97 if (!transport.stream) return;
98 assert.isNull(err);
99 assert.isNotNull(log.message);
100 log.stream.destroy();
101 }
102 }
103 },
104 'after the logs have flushed': {
105 topic: function () {
106 setTimeout(this.callback, 1000);
107 },
108 'the query() method': {
109 'using basic querying': {
110 'topic': function () {
111 if (!transport.query) return;
112 var cb = this.callback;
113 logger.log('info', 'hello world', {}, function () {
114 logger.query(cb);
115 });
116 },
117 'should return matching results': function (err, results) {
118 if (!transport.query) return;
119 assert.isNull(err);
120 results = results[transport.name];
121 while (!Array.isArray(results)) {
122 results = results[Object.keys(results).pop()];
123 }
124 var log = results.pop();
125 assert.ok(log.message.indexOf('hello world') === 0
126 || log.message.indexOf('test message') === 0);
127 }
128 },
129 'using the `rows` option': {
130 'topic': function () {
131 if (!transport.query) return;
132 var cb = this.callback;
133 logger.log('info', 'hello world', {}, function () {
134 logger.query({ rows: 1 }, cb);
135 });
136 },
137 'should return one result': function (err, results) {
138 if (!transport.query) return;
139 assert.isNull(err);
140 results = results[transport.name];
141 while (!Array.isArray(results)) {
142 results = results[Object.keys(results).pop()];
143 }
144 assert.equal(results.length, 1);
145 }
146 },
147 'using `fields` and `order` option': {
148 'topic': function () {
149 if (!transport.query) return;
150 var cb = this.callback;
151 logger.log('info', 'hello world', {}, function () {
152 logger.query({ order: 'asc', fields: ['timestamp'] }, cb);
153 });
154 },
155 'should return matching results': function (err, results) {
156 if (!transport.query) return;
157 assert.isNull(err);
158 results = results[transport.name];
159 while (!Array.isArray(results)) {
160 results = results[Object.keys(results).pop()];
161 }
162 assert.equal(Object.keys(results[0]).length, 1);
163 assert.ok(new Date(results.shift().timestamp)
164 < new Date(results.pop().timestamp));
165 }
166 },
167 'using the `from` and `until` option': {
168 'topic': function () {
169 if (!transport.query) return;
170 var cb = this.callback;
171 var start = Date.now() - (100 * 1000);
172 var end = Date.now() + (100 * 1000);
173 logger.query({ from: start, until: end }, cb);
174 },
175 'should return matching results': function (err, results) {
176 if (!transport.query) return;
177 assert.isNull(err);
178 results = results[transport.name];
179 while (!Array.isArray(results)) {
180 results = results[Object.keys(results).pop()];
181 }
182 assert.ok(results.length >= 1);
183 }
184 },
185 'using a bad `from` and `until` option': {
186 'topic': function () {
187 if (!transport.query) return;
188 var cb = this.callback;
189 logger.log('info', 'bad from and until', {}, function () {
190 var now = Date.now() + 1000000;
191 logger.query({ from: now, until: now }, cb);
192 });
193 },
194 'should return no results': function (err, results) {
195 if (!transport.query) return;
196 assert.isNull(err);
197 results = results[transport.name];
198 while (!Array.isArray(results)) {
199 results = results[Object.keys(results).pop()];
200 }
201 results = [results.filter(function(log) {
202 return log.message === 'bad from and until';
203 }).pop()];
204 assert.isUndefined(results[0]);
205 }
206 }
207 }
208 }
209 };
210
211 return out;
212};