Skip to content

Commit

Permalink
validate options.level{s} and add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ofrobots committed Jan 3, 2017
1 parent fd43236 commit a3ee52a
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 5 deletions.
18 changes: 17 additions & 1 deletion packages/logging-winston/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,30 @@ function LoggingWinston(options) {

options = options || {};

// options.levels must be a map from level names to stackdriver severities.
if (options.levels) {
for (var key in options.levels) {
var level = options.levels[key];
if (!is.number(level) || level < 0 || level > 7) {
throw new Error('invalid options.levels: ' + key + ':' + level);
}
}
}

this.levels_ = options.levels || NPM_LEVEL_NAME_TO_CODE;

// options.level must be a valid entry from the levels_.
if (options.level && !this.levels_[options.level]) {
throw new Error('invalid options.level: ' + options.level);
}

var logName = options.logName || 'winston_log';

winston.Transport.call(this, {
level: options.level,
name: logName
});

this.levels_ = options.levels || NPM_LEVEL_NAME_TO_CODE;
this.log_ = logging(options).log(logName);
this.resource_ = options.resource;
}
Expand Down
103 changes: 99 additions & 4 deletions packages/logging-winston/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,27 @@
var assert = require('assert');
var extend = require('extend');
var proxyquire = require('proxyquire');
var util = require('util');

describe('logging-winston', function() {
var fakeLogInstance = {};
var fakeLogEntry_;
var fakeLogLevel_;

var fakeLogInstance = {
entry: function(metadata, message) {
return {metadata: metadata, message: message}
},
};

['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info',
'debug']
.forEach(function(level) {
fakeLogInstance[level] = function(entry, cb) {
fakeLogLevel_ = level;
fakeLogEntry_ = entry;
setImmediate(cb);
}
});

var fakeLoggingOptions_;
var fakeLogName_;
Expand Down Expand Up @@ -49,8 +67,8 @@ describe('logging-winston', function() {
var loggingWinston;

var OPTIONS = {
level: 'level',
levels: [1, 2, 3],
level: 'levelThree',
levels: {levelOne: 1, levelTwo: 2, levelThree: 3, levelFour: 4},
logName: 'log-name',
resource: {}
};
Expand All @@ -63,6 +81,8 @@ describe('logging-winston', function() {
});

beforeEach(function() {
fakeLogEntry_ = null;
fakeLogLevel_ = null;
fakeLoggingOptions_ = null;
fakeLogName_ = null;
loggingWinston = new LoggingWinston(OPTIONS);
Expand Down Expand Up @@ -96,6 +116,7 @@ describe('logging-winston', function() {
it('should default to npm levels', function() {
var optionsWithoutLevels = extend({}, OPTIONS);
delete optionsWithoutLevels.levels;
delete optionsWithoutLevels.level;

var loggingWinston = new LoggingWinston(optionsWithoutLevels);
assert.deepEqual(loggingWinston.levels_, {
Expand Down Expand Up @@ -128,7 +149,81 @@ describe('logging-winston', function() {
it('should localize the provided resource', function() {
assert.strictEqual(loggingWinston.resource_, OPTIONS.resource);
});

it('should throw on invalid options.level', function() {
var optionsWithoutLevels = extend({}, OPTIONS);
delete optionsWithoutLevels.levels;

assert.throws(function () {
new LoggingWinston(optionsWithoutLevels);
}, /invalid options\.level: levelThree/);

assert.throws(function () {
new LoggingWinston(extend({}, OPTIONS, {level: 'err'}));
}, /invalid options\.level: err/);

});

it('should throw on invalid options.levels', function() {
assert.throws(function() {
new LoggingWinston(
extend({}, OPTIONS, {levels: {a: 0, b: 1, c: 3, d: 6, e: 9}}));
}, /invalid options\.levels: e:9/);
assert.throws(function() {
new LoggingWinston(
extend({}, OPTIONS, {levels: {9: 0, 7: 1, 8: 3, 1: 'foo', 0: 6}}));
}, /invalid options\.levels: 1:foo/);
});
});

describe('log', function() {});
describe('log', function() {
it('should throw on a bad log level', function() {
assert.throws(function() {
loggingWinston.log('non-existent-level', 'test log message');
}, /Unknown log level/);
});

it('should call back once completed', function(done) {
loggingWinston.log('levelOne', 'level one message', function(err) {
assert.ifError(err);
done();
});
});

it('should call into stackdriver logging properly formatted entry',
function(done) {
var message = 'a message to be logged';
loggingWinston.log('levelOne', message, function(err) {
assert.strictEqual(fakeLogLevel_, 'alert');
assert.deepEqual(fakeLogEntry_, {
message: message,
metadata: {resource: OPTIONS.resource, labels: {}}
});
done();
});
});

it('should convert metadata values using util.inspect', function(done) {
var message = 'test message with labels';
var metadata = {
testDate: new Date(1),
testFunction: function foo() { return 'some value'; },
deepObject: {a: {b: {c: {d: {e: 1}}}}},
testNumber: 5
};
loggingWinston.log('levelTwo', message, metadata, function(err) {
assert.strictEqual(fakeLogLevel_, 'critical');
var expectedLabels = {};
for (var key in metadata) {
expectedLabels[key] = util.inspect(metadata[key]);
}
assert.deepEqual(fakeLogEntry_, {
message: message,
metadata: {resource: OPTIONS.resource, labels: expectedLabels}
});
done();
});
});

});
});

0 comments on commit a3ee52a

Please sign in to comment.