Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
/*
2
* logger-test.js: Tests for instances of the winston Logger
3
*
4
* (C) 2010 Charlie Robbins
5
* MIT LICENSE
6
*
7
*/
8
9
var path = require('path'),
10
vows = require('vows'),
11
assert = require('assert'),
12
winston = require('../lib/winston'),
13
helpers = require('./helpers'),
14
transport = require('./transports/transport');
15
16
vows.describe('winton/logger').addBatch({
17
"An instance of winston.Logger": {
18
topic: new (winston.Logger)({ transports: [new (winston.transports.Console)({ level: 'info' })] }),
19
"should have the correct methods / properties defined": function (logger) {
20
helpers.assertLogger(logger);
21
},
22
"the add() with an unsupported transport": {
23
"should throw an error": function () {
24
assert.throws(function () { logger.add('unsupported') }, Error);
25
}
26
}
27
}
28
}).addBatch({
29
"An instance of winston.Logger with no transports": {
30
topic: new (winston.Logger)({ emitErrs: true }),
31
"the log() method should throw an error": function (logger) {
32
assert.throws(function () { logger.log('anything') }, Error);
33
},
34
"the extend() method called on an empty object": {
35
topic: function (logger) {
36
var empty = {};
37
logger.extend(empty);
38
return empty;
39
},
40
"should define the appropriate methods": function (extended) {
41
['log', 'profile', 'startTimer'].concat(Object.keys(winston.config.npm.levels)).forEach(function (method) {
42
assert.isFunction(extended[method]);
43
});
44
}
45
},
46
"the add() method with a supported transport": {
47
topic: function (logger) {
48
return logger.add(winston.transports.Console);
49
},
50
"should add the console Transport onto transports": function (logger) {
51
assert.equal(helpers.size(logger.transports), 1);
52
helpers.assertConsole(logger.transports.console);
53
},
54
"should throw an error when the same Transport is added": function (logger) {
55
assert.throws(function () { logger.add(winston.transports.Console) }, Error);
56
},
57
"the log() method": {
58
topic: function (logger) {
59
logger.once('logging', this.callback);
60
logger.log('info', 'test message');
61
},
62
"should emit the 'log' event with the appropriate transport": function (transport, ign) {
63
helpers.assertConsole(transport);
64
}
65
},
66
"the profile() method": {
67
"when passed a callback": {
68
topic: function (logger) {
69
var that = this;
70
logger.profile('test1');
71
setTimeout(function () {
72
logger.profile('test1', function (err, level, msg, meta) {
73
that.callback(err, level, msg, meta, logger);
74
});
75
}, 1000);
76
},
77
"should respond with the appropriate profile message": function (err, level, msg, meta, logger) {
78
assert.isNull(err);
79
assert.equal(level, 'info');
80
assert.match(meta.duration, /(\d+)ms/);
81
assert.isTrue(typeof logger.profilers['test'] === 'undefined');
82
}
83
},
84
"when not passed a callback": {
85
topic: function (logger) {
86
var that = this;
87
logger.profile('test2');
88
logger.once('logging', that.callback.bind(null, null));
89
setTimeout(function () {
90
logger.profile('test2');
91
}, 1000);
92
},
93
"should respond with the appropriate profile message": function (err, transport, level, msg, meta) {
94
assert.isNull(err);
95
assert.equal(level, 'info');
96
assert.match(meta.duration, /(\d+)ms/);
97
}
98
}
99
},
100
"the startTimer() method": {
101
"when passed a callback": {
102
topic: function (logger) {
103
var that = this;
104
var timer = logger.startTimer()
105
setTimeout(function () {
106
timer.done('test', function (err, level, msg, meta) {
107
that.callback(err, level, msg, meta, logger);
108
});
109
}, 1000);
110
},
111
"should respond with the appropriate message": function (err, level, msg, meta, logger) {
112
assert.isNull(err);
113
assert.equal(level, 'info');
114
assert.match(meta.duration, /(\d+)ms/);
115
}
116
},
117
"when not passed a callback": {
118
topic: function (logger) {
119
var that = this;
120
var timer = logger.startTimer()
121
logger.once('logging', that.callback.bind(null, null));
122
setTimeout(function () {
123
timer.done();
124
}, 1000);
125
},
126
"should respond with the appropriate message": function (err, transport, level, msg, meta) {
127
assert.isNull(err);
128
assert.equal(level, 'info');
129
assert.match(meta.duration, /(\d+)ms/);
130
131
var duration = parseInt(meta.duration);
132
assert.isNumber(duration);
133
assert.isTrue(duration > 900 && duration < 1100);
134
}
135
}
136
},
137
"and adding an additional transport": {
138
topic: function (logger) {
139
return logger.add(winston.transports.File, {
140
filename: path.join(__dirname, 'fixtures', 'logs', 'testfile2.log')
141
});
142
},
143
"should be able to add multiple transports": function (logger) {
144
assert.equal(helpers.size(logger.transports), 2);
145
helpers.assertConsole(logger.transports.console);
146
helpers.assertFile(logger.transports.file);
147
}
148
}
149
}
150
}
151
}).addBatch({
152
"The winston logger": {
153
topic: new (winston.Logger)({
154
transports: [
155
new (winston.transports.Console)(),
156
new (winston.transports.File)({ filename: path.join(__dirname, 'fixtures', 'logs', 'filelog.log' )})
157
]
158
}),
159
"should return have two transports": function (logger) {
160
assert.equal(helpers.size(logger.transports), 2);
161
},
162
"the remove() with an unadded transport": {
163
"should throw an Error": function (logger) {
164
assert.throws(function () { logger.remove(winston.transports.Webhook) }, Error);
165
}
166
},
167
"the remove() method with an added transport": {
168
topic: function (logger) {
169
return logger.remove(winston.transports.Console);
170
},
171
"should remove the Console transport from transports": function (logger) {
172
assert.equal(helpers.size(logger.transports), 1);
173
helpers.assertFile(logger.transports.file);
174
},
175
"and removing an additional transport": {
176
topic: function (logger) {
177
return logger.remove(winston.transports.File);
178
},
179
"should remove File transport from transports": function (logger) {
180
assert.equal(helpers.size(logger.transports), 0);
181
}
182
}
183
}
184
}
185
}).addBatch({
186
"The winston logger": {
187
topic: new (winston.Logger)({
188
transports: [
189
new (winston.transports.Console)(),
190
new (winston.transports.File)({ filename: path.join(__dirname, 'fixtures', 'logs', 'filelog.log' )})
191
]
192
}),
193
"the clear() method": {
194
"should remove all transports": function (logger) {
195
logger.clear();
196
assert.equal(helpers.size(logger.transports), 0);
197
}
198
}
199
}
200
}).export(module);
201
202