Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit d6e6809

Browse files
nchiladamikeproeng37
authored andcommitted
fix(default logger): log each message at the correct level (optimizely#209)
1 parent fe6916f commit d6e6809

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

packages/optimizely-sdk/lib/plugins/logger/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
var fns = require('../../utils/fns');
1717
var enums = require('../../utils/enums');
18+
var sprintf = require('sprintf-js').sprintf;
19+
20+
var MODULE_NAME = 'DEFAULT_LOGGER';
1821

1922
/**
2023
* Default logger implementation
@@ -37,9 +40,9 @@ function Logger(config) {
3740
prefix: '[OPTIMIZELY]',
3841
}, config);
3942

40-
this.setLogLevel(config.logLevel);
4143
this.logToConsole = config.logToConsole;
4244
this.prefix = config.prefix;
45+
this.setLogLevel(config.logLevel);
4346
}
4447

4548
/**
@@ -50,7 +53,11 @@ function Logger(config) {
5053
Logger.prototype.log = function(logLevel, logMessage) {
5154
if (this.__shouldLog(logLevel)) {
5255
if (this.prefix) {
53-
logMessage = this.prefix + ' - ' + this.logLevelName + ' ' + getTime() + ' ' + logMessage;
56+
var logLevelName = getLogLevelName(logLevel);
57+
logMessage = sprintf(
58+
enums.DEFAULT_LOGGER_MESSAGE_TEMPLATE,
59+
this.prefix, logLevelName, getTime(), logMessage
60+
);
5461
}
5562

5663
if (this.logToConsole) {
@@ -66,8 +73,8 @@ Logger.prototype.log = function(logLevel, logMessage) {
6673
Logger.prototype.setLogLevel = function(logLevel) {
6774
// Check that logLevel is valid, otherwise default to ERROR
6875
this.logLevel = (fns.values(enums.LOG_LEVEL).indexOf(logLevel) > -1) ? logLevel : enums.LOG_LEVEL.ERROR;
69-
this.logLevelName = getLogLevelName(this.logLevel);
70-
this.log('Setting log level to ' + logLevel);
76+
var logLevelName = getLogLevelName(this.logLevel);
77+
this.log(enums.LOG_LEVEL.DEBUG, sprintf(enums.LOG_MESSAGES.SET_LOG_LEVEL, MODULE_NAME, logLevelName));
7178
};
7279

7380
/**
@@ -93,7 +100,7 @@ Logger.prototype.__consoleLog = function(logLevel, logArguments) {
93100
console.log.apply(console, logArguments);
94101
break;
95102
case enums.LOG_LEVEL.INFO:
96-
console.log.apply(console, logArguments);
103+
console.info.apply(console, logArguments);
97104
break;
98105
case enums.LOG_LEVEL.WARNING:
99106
console.warn.apply(console, logArguments);

packages/optimizely-sdk/lib/plugins/logger/index.tests.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,47 @@ describe('lib/plugins/logger', function() {
3535
describe('log', function() {
3636
beforeEach(function() {
3737
defaultLogger = logger.createLogger({logLevel: LOG_LEVEL.INFO});
38-
sinon.stub(defaultLogger, '__consoleLog');
38+
39+
sinon.stub(console, 'log');
40+
sinon.stub(console, 'info');
41+
sinon.stub(console, 'warn');
42+
sinon.stub(console, 'error');
43+
});
44+
45+
afterEach(function() {
46+
console.log.restore();
47+
console.info.restore();
48+
console.warn.restore();
49+
console.error.restore();
3950
});
4051

41-
it('should log the given message', function() {
52+
it('should log a message at the threshold log level', function() {
4253
defaultLogger.log(LOG_LEVEL.INFO, 'message');
43-
assert.isTrue(defaultLogger.__consoleLog.calledOnce);
44-
assert.notStrictEqual(defaultLogger.__consoleLog.firstCall.args, [LOG_LEVEL.INFO, ['message']]);
54+
55+
sinon.assert.notCalled(console.log);
56+
sinon.assert.calledOnce(console.info);
57+
sinon.assert.calledWithExactly(console.info, sinon.match(/.*INFO.*message.*/));
58+
sinon.assert.notCalled(console.warn);
59+
sinon.assert.notCalled(console.error);
4560
});
4661

47-
it('should not log the message if the log level is lower than the current log level', function() {
62+
it('should log a message if its log level is higher than the threshold log level', function() {
63+
defaultLogger.log(LOG_LEVEL.WARNING, 'message');
64+
65+
sinon.assert.notCalled(console.log);
66+
sinon.assert.notCalled(console.info);
67+
sinon.assert.calledOnce(console.warn);
68+
sinon.assert.calledWithExactly(console.warn, sinon.match(/.*WARNING.*message.*/));
69+
sinon.assert.notCalled(console.error);
70+
});
71+
72+
it('should not log a message if its log level is lower than the threshold log level', function() {
4873
defaultLogger.log(LOG_LEVEL.DEBUG, 'message');
49-
assert.isTrue(defaultLogger.__consoleLog.notCalled);
74+
75+
sinon.assert.notCalled(console.log);
76+
sinon.assert.notCalled(console.info);
77+
sinon.assert.notCalled(console.warn);
78+
sinon.assert.notCalled(console.error);
5079
});
5180
});
5281

packages/optimizely-sdk/lib/utils/enums/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ exports.LOG_MESSAGES = {
8888
ROLLOUT_HAS_NO_EXPERIMENTS: '%s: Rollout of feature %s has no experiments',
8989
SAVED_VARIATION: '%s: Saved variation "%s" of experiment "%s" for user "%s".',
9090
SAVED_VARIATION_NOT_FOUND: '%s: User %s was previously bucketed into variation with ID %s for experiment %s, but no matching variation was found.',
91+
SET_LOG_LEVEL: '%s: Setting log level to "%s"',
9192
SHOULD_NOT_DISPATCH_ACTIVATE: '%s: Experiment %s is in "Launched" state. Not activating user.',
9293
SKIPPING_JSON_VALIDATION: '%s: Skipping JSON schema validation.',
9394
TRACK_EVENT: '%s: Tracking event %s for user %s.',
@@ -125,6 +126,8 @@ exports.LOG_MESSAGES = {
125126
BUCKETING_ID_NOT_STRING: '%s: BucketingID attribute is not a string. Defaulted to userId',
126127
};
127128

129+
exports.DEFAULT_LOGGER_MESSAGE_TEMPLATE = '%s - %s %s %s';
130+
128131
exports.RESERVED_EVENT_KEYWORDS = {
129132
REVENUE: 'revenue',
130133
VALUE: 'value',

0 commit comments

Comments
 (0)