Skip to content

Commit a8bdbb4

Browse files
committed
validate options.level{s} and add some tests
1 parent fd43236 commit a8bdbb4

File tree

2 files changed

+122
-5
lines changed

2 files changed

+122
-5
lines changed

packages/logging-winston/src/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,31 @@ function LoggingWinston(options) {
104104

105105
options = options || {};
106106

107+
// options.levels must be a map from level names to stackdriver severities.
108+
if (options.levels) {
109+
for (var key in options.levels) {
110+
var level = options.levels[key];
111+
if (!is.number(level) || level < 0 || level > 7) {
112+
throw new Error('invalid options.levels: ' + key + ':' + level);
113+
}
114+
}
115+
}
116+
117+
this.levels_ = options.levels || NPM_LEVEL_NAME_TO_CODE;
118+
119+
// options.level must be a valid entry from the levels_.
120+
if (options.level && !this.levels_[options.level]) {
121+
throw new Error('invalid options.level: ' + options.level);
122+
}
123+
107124
var logName = options.logName || 'winston_log';
108125

109126
winston.Transport.call(this, {
110127
level: options.level,
111128
name: logName
112129
});
113130

114-
this.levels_ = options.levels || NPM_LEVEL_NAME_TO_CODE;
131+
115132
this.log_ = logging(options).log(logName);
116133
this.resource_ = options.resource;
117134
}

packages/logging-winston/test/index.js

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,32 @@
1919
var assert = require('assert');
2020
var extend = require('extend');
2121
var proxyquire = require('proxyquire');
22+
var util = require('util');
2223

2324
describe('logging-winston', function() {
24-
var fakeLogInstance = {};
25+
var fakeLogEntry_;
26+
var fakeLogLevel_;
27+
28+
var loggingFunction = function(entry, cb) {
29+
lastLogEntry = entry;
30+
if (cb) { cb(null); }
31+
};
32+
33+
var fakeLogInstance = {
34+
entry: function(metadata, message) {
35+
return {metadata: metadata, message: message}
36+
},
37+
};
38+
39+
['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info',
40+
'debug']
41+
.forEach(function(level) {
42+
fakeLogInstance[level] = function(entry, cb) {
43+
fakeLogLevel_ = level;
44+
fakeLogEntry_ = entry;
45+
setImmediate(cb);
46+
}
47+
});
2548

2649
var fakeLoggingOptions_;
2750
var fakeLogName_;
@@ -49,8 +72,8 @@ describe('logging-winston', function() {
4972
var loggingWinston;
5073

5174
var OPTIONS = {
52-
level: 'level',
53-
levels: [1, 2, 3],
75+
level: 'levelThree',
76+
levels: {levelOne: 1, levelTwo: 2, levelThree: 3, levelFour: 4},
5477
logName: 'log-name',
5578
resource: {}
5679
};
@@ -63,6 +86,8 @@ describe('logging-winston', function() {
6386
});
6487

6588
beforeEach(function() {
89+
fakeLogEntry_ = null;
90+
fakeLogLevel_ = null;
6691
fakeLoggingOptions_ = null;
6792
fakeLogName_ = null;
6893
loggingWinston = new LoggingWinston(OPTIONS);
@@ -96,6 +121,7 @@ describe('logging-winston', function() {
96121
it('should default to npm levels', function() {
97122
var optionsWithoutLevels = extend({}, OPTIONS);
98123
delete optionsWithoutLevels.levels;
124+
delete optionsWithoutLevels.level;
99125

100126
var loggingWinston = new LoggingWinston(optionsWithoutLevels);
101127
assert.deepEqual(loggingWinston.levels_, {
@@ -128,7 +154,81 @@ describe('logging-winston', function() {
128154
it('should localize the provided resource', function() {
129155
assert.strictEqual(loggingWinston.resource_, OPTIONS.resource);
130156
});
157+
158+
it('should throw on invalid options.level', function() {
159+
var optionsWithoutLevels = extend({}, OPTIONS);
160+
delete optionsWithoutLevels.levels;
161+
162+
assert.throws(function () {
163+
new LoggingWinston(optionsWithoutLevels);
164+
}, /invalid options\.level: levelThree/);
165+
166+
assert.throws(function () {
167+
new LoggingWinston(extend({}, OPTIONS, {level: 'err'}));
168+
}, /invalid options\.level: err/);
169+
170+
});
171+
172+
it('should throw on invalid options.levels', function() {
173+
assert.throws(function() {
174+
new LoggingWinston(
175+
extend({}, OPTIONS, {levels: {a: 0, b: 1, c: 3, d: 6, e: 9}}));
176+
}, /invalid options\.levels: e:9/);
177+
assert.throws(function() {
178+
new LoggingWinston(
179+
extend({}, OPTIONS, {levels: {9: 0, 7: 1, 8: 3, 1: 'foo', 0: 6}}));
180+
}, /invalid options\.levels: 1:foo/);
181+
});
131182
});
132183

133-
describe('log', function() {});
184+
describe('log', function() {
185+
it('should throw on a bad log level', function() {
186+
assert.throws(function() {
187+
loggingWinston.log('non-existent-level', 'test log message');
188+
}, /Unknown log level/);
189+
});
190+
191+
it('should call back once completed', function(done) {
192+
loggingWinston.log('levelOne', 'level one message', function(err) {
193+
assert.ifError(err);
194+
done();
195+
});
196+
});
197+
198+
it('should call into stackdriver logging properly formatted entry',
199+
function(done) {
200+
var message = 'a message to be logged';
201+
loggingWinston.log('levelOne', message, function(err) {
202+
assert.strictEqual(fakeLogLevel_, 'alert');
203+
assert.deepEqual(fakeLogEntry_, {
204+
message: message,
205+
metadata: {resource: OPTIONS.resource, labels: {}}
206+
});
207+
done();
208+
});
209+
});
210+
211+
it('should convert metadata values using util.inspect', function(done) {
212+
var message = 'test message with labels';
213+
var metadata = {
214+
testDate: new Date(1),
215+
testFunction: function foo() { return 'some value'; },
216+
deepObject: {a: {b: {c: {d: {e: 1}}}}},
217+
testNumber: 5
218+
};
219+
loggingWinston.log('levelTwo', message, metadata, function(err) {
220+
assert.strictEqual(fakeLogLevel_, 'critical');
221+
var expectedLabels = {};
222+
for (var key in metadata) {
223+
expectedLabels[key] = util.inspect(metadata[key]);
224+
}
225+
assert.deepEqual(fakeLogEntry_, {
226+
message: message,
227+
metadata: {resource: OPTIONS.resource, labels: expectedLabels}
228+
});
229+
done();
230+
});
231+
});
232+
233+
});
134234
});

0 commit comments

Comments
 (0)