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

Commit 8df9235

Browse files
wrap optimizely sprintf
1 parent 5a376da commit 8df9235

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

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

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var FEATURE_VARIABLE_TYPES = enums.FEATURE_VARIABLE_TYPES;
5050
function Optimizely(config) {
5151
var clientEngine = config.clientEngine;
5252
if (clientEngine !== enums.NODE_CLIENT_ENGINE && clientEngine !== enums.JAVASCRIPT_CLIENT_ENGINE) {
53-
config.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.INVALID_CLIENT_ENGINE, MODULE_NAME, clientEngine));
53+
config.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.INVALID_CLIENT_ENGINE, MODULE_NAME, {clientEngine}));
5454
clientEngine = enums.NODE_CLIENT_ENGINE;
5555
}
5656

@@ -116,7 +116,7 @@ function Optimizely(config) {
116116
Optimizely.prototype.activate = function (experimentKey, userId, attributes) {
117117
try {
118118
if (!this.isValidInstance) {
119-
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'activate'));
119+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, { active: 'activate'}));
120120
return null;
121121
}
122122

@@ -132,7 +132,7 @@ Optimizely.prototype.activate = function (experimentKey, userId, attributes) {
132132

133133
// If experiment is not set to 'Running' status, log accordingly and return variation key
134134
if (!projectConfig.isRunning(this.configObj, experimentKey)) {
135-
var shouldNotDispatchActivateLogMessage = sprintf(LOG_MESSAGES.SHOULD_NOT_DISPATCH_ACTIVATE, MODULE_NAME, experimentKey);
135+
var shouldNotDispatchActivateLogMessage = sprintf(LOG_MESSAGES.SHOULD_NOT_DISPATCH_ACTIVATE, MODULE_NAME, {experimentKey});
136136
this.logger.log(LOG_LEVEL.DEBUG, shouldNotDispatchActivateLogMessage);
137137
return variationKey;
138138
}
@@ -142,7 +142,7 @@ Optimizely.prototype.activate = function (experimentKey, userId, attributes) {
142142
return variationKey;
143143
} catch (ex) {
144144
this.logger.log(LOG_LEVEL.ERROR, ex.message);
145-
var failedActivationLogMessage = sprintf(LOG_MESSAGES.NOT_ACTIVATING_USER, MODULE_NAME, userId, experimentKey);
145+
var failedActivationLogMessage = sprintf(LOG_MESSAGES.NOT_ACTIVATING_USER, MODULE_NAME, {userId, experimentKey});
146146
this.logger.log(LOG_LEVEL.INFO, failedActivationLogMessage);
147147
this.errorHandler.handleError(ex);
148148
return null;
@@ -183,7 +183,7 @@ Optimizely.prototype._sendImpressionEvent = function(experimentKey, variationKey
183183
JSON.stringify(impressionEvent.params));
184184
this.logger.log(LOG_LEVEL.DEBUG, dispatchedImpressionEventLogMessage);
185185
var eventDispatcherCallback = function() {
186-
var activatedLogMessage = sprintf(LOG_MESSAGES.ACTIVATE_USER, MODULE_NAME, userId, experimentKey);
186+
var activatedLogMessage = sprintf(LOG_MESSAGES.ACTIVATE_USER, MODULE_NAME, {userId, experimentKey});
187187
this.logger.log(LOG_LEVEL.INFO, activatedLogMessage);
188188
}.bind(this);
189189
this.__dispatchEvent(impressionEvent, eventDispatcherCallback);
@@ -216,7 +216,7 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
216216
try {
217217

218218
if (!this.isValidInstance) {
219-
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'track'));
219+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, {track: 'track'}));
220220
return;
221221
}
222222

@@ -231,7 +231,7 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
231231
// Return and do not send conversion events if the event is not associated with any running experiments
232232
this.logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.EVENT_NOT_ASSOCIATED_WITH_EXPERIMENTS,
233233
MODULE_NAME,
234-
eventKey));
234+
{eventKey}));
235235
return;
236236
}
237237

@@ -253,12 +253,12 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
253253

254254
var dispatchedConversionEventLogMessage = sprintf(LOG_MESSAGES.DISPATCH_CONVERSION_EVENT,
255255
MODULE_NAME,
256-
conversionEvent.url,
257-
JSON.stringify(conversionEvent.params));
256+
{ url: conversionEvent.url,
257+
converstion: JSON.stringify(conversionEvent.params), {conversionEvent});
258258
this.logger.log(LOG_LEVEL.DEBUG, dispatchedConversionEventLogMessage);
259259

260260
var eventDispatcherCallback = function () {
261-
var trackedLogMessage = sprintf(LOG_MESSAGES.TRACK_EVENT, MODULE_NAME, eventKey, userId);
261+
var trackedLogMessage = sprintf(LOG_MESSAGES.TRACK_EVENT, MODULE_NAME, {eventKey, userId});
262262
this.logger.log(LOG_LEVEL.INFO, trackedLogMessage);
263263
}.bind(this);
264264

@@ -276,7 +276,7 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
276276
);
277277
} catch (ex) {
278278
this.logger.log(LOG_LEVEL.ERROR, ex.message);
279-
var failedTrackLogMessage = sprintf(LOG_MESSAGES.NOT_TRACKING_USER, MODULE_NAME, userId);
279+
var failedTrackLogMessage = sprintf(LOG_MESSAGES.NOT_TRACKING_USER, MODULE_NAME, {userId});
280280
this.logger.log(LOG_LEVEL.INFO, failedTrackLogMessage);
281281
this.errorHandler.handleError(ex);
282282
}
@@ -297,7 +297,7 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
297297
Optimizely.prototype.getVariation = function(experimentKey, userId, attributes) {
298298
try {
299299
if (!this.isValidInstance) {
300-
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getVariation'));
300+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, {getVariation: 'getVariation'}));
301301
return null;
302302
}
303303

@@ -308,7 +308,7 @@ Optimizely.prototype.getVariation = function(experimentKey, userId, attributes)
308308

309309
var experiment = this.configObj.experimentKeyMap[experimentKey];
310310
if (fns.isEmpty(experiment)) {
311-
this.logger.log(LOG_LEVEL.DEBUG, sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey));
311+
this.logger.log(LOG_LEVEL.DEBUG, sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, {experimentKey}));
312312
return null;
313313
}
314314

@@ -380,7 +380,7 @@ Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, e
380380
if (stringInputs.hasOwnProperty('user_id')) {
381381
var userId = stringInputs.user_id;
382382
if (typeof userId !== 'string' || userId === null || userId === 'undefined') {
383-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, 'user_id'));
383+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, { key: 'user_id'}));
384384
}
385385

386386
delete stringInputs.user_id;
@@ -390,7 +390,7 @@ Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, e
390390
for (var index = 0; index < inputKeys.length; index++) {
391391
var key = inputKeys[index];
392392
if (!stringValidator.validate(stringInputs[key])) {
393-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, key));
393+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, {key}));
394394
}
395395
}
396396
if (userAttributes) {
@@ -434,7 +434,7 @@ Optimizely.prototype.__getValidExperimentsForEvent = function(eventKey, userId,
434434
if (variationKey) {
435435
// if experiment is active but not running, it is in LAUNCHED state, so we don't track a conversion for it
436436
if (!projectConfig.isRunning(this.configObj, experimentKey)) {
437-
var shouldNotDispatchTrackLogMessage = sprintf(LOG_MESSAGES.SHOULD_NOT_DISPATCH_TRACK, MODULE_NAME, experimentKey);
437+
var shouldNotDispatchTrackLogMessage = sprintf(LOG_MESSAGES.SHOULD_NOT_DISPATCH_TRACK, MODULE_NAME, {experimentKey});
438438
this.logger.log(LOG_LEVEL.DEBUG, shouldNotDispatchTrackLogMessage);
439439
} else {
440440
// if running + user is bucketed then add to result
@@ -444,8 +444,9 @@ Optimizely.prototype.__getValidExperimentsForEvent = function(eventKey, userId,
444444
} else {
445445
var notTrackingUserForExperimentLogMessage = sprintf(LOG_MESSAGES.NOT_TRACKING_USER_FOR_EXPERIMENT,
446446
MODULE_NAME,
447+
{
447448
userId,
448-
experimentKey);
449+
experimentKey});
449450
this.logger.log(LOG_LEVEL.DEBUG, notTrackingUserForExperimentLogMessage);
450451
}
451452
return results;
@@ -461,7 +462,7 @@ Optimizely.prototype.__getValidExperimentsForEvent = function(eventKey, userId,
461462
* @return {null}
462463
*/
463464
Optimizely.prototype.__notActivatingExperiment = function(experimentKey, userId) {
464-
var failedActivationLogMessage = sprintf(LOG_MESSAGES.NOT_ACTIVATING_USER, MODULE_NAME, userId, experimentKey);
465+
var failedActivationLogMessage = sprintf(LOG_MESSAGES.NOT_ACTIVATING_USER, MODULE_NAME, {userId, experimentKey});
465466
this.logger.log(LOG_LEVEL.INFO, failedActivationLogMessage);
466467
return null;
467468
};
@@ -505,7 +506,7 @@ Optimizely.prototype.__filterEmptyValues = function (map) {
505506
Optimizely.prototype.isFeatureEnabled = function (featureKey, userId, attributes) {
506507
try {
507508
if (!this.isValidInstance) {
508-
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'isFeatureEnabled'));
509+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, {key: 'isFeatureEnabled'}));
509510
return false;
510511
}
511512

@@ -526,11 +527,11 @@ Optimizely.prototype.isFeatureEnabled = function (featureKey, userId, attributes
526527
this._sendImpressionEvent(decision.experiment.key, decision.variation.key, userId, attributes);
527528
}
528529
if (variation.featureEnabled === true) {
529-
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId));
530+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, {featureKey, userId}));
530531
return true;
531532
}
532533
}
533-
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId));
534+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, {featureKey, userId}));
534535
return false;
535536
} catch (e) {
536537
this.logger.log(LOG_LEVEL.ERROR, e.message);
@@ -550,7 +551,7 @@ Optimizely.prototype.getEnabledFeatures = function (userId, attributes) {
550551
try {
551552
var enabledFeatures = [];
552553
if (!this.isValidInstance) {
553-
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getEnabledFeatures'));
554+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, {key: 'getEnabledFeatures'}));
554555
return enabledFeatures;
555556
}
556557

@@ -614,18 +615,18 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
614615
}
615616

616617
if (variable.type !== variableType) {
617-
this.logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, variableType, variable.type));
618+
this.logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, {variableType, type: variable.type}));
618619
return null;
619620
}
620621

621622
var decision = this.decisionService.getVariationForFeature(featureFlag, userId, attributes);
622623
var variableValue;
623624
if (decision.variation !== null) {
624625
variableValue = projectConfig.getVariableValueForVariation(this.configObj, variable, decision.variation, this.logger);
625-
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE, MODULE_NAME, variableKey, featureFlag.key, variableValue, userId));
626+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE, MODULE_NAME, {variableKey, featureFlagKey: featureFlag.key, variableValue, userId}));
626627
} else {
627628
variableValue = variable.defaultValue;
628-
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE, MODULE_NAME, userId, variableKey, featureFlag.key));
629+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE, MODULE_NAME, {userId, variableKey, featureFlagKey: featureFlag.key}));
629630
}
630631

631632
return projectConfig.getTypeCastValue(variableValue, variableType, this.logger);

packages/optimizely-sdk/lib/utils/sprintf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const sprintf2 = require('sprintf-js').sprintf;
22

33
const sprintf = (message, moduleName, data) => {
4+
if (typeof data === 'string') return;
45
const dataValues = Object.keys(data).map(k => data[k]);
56
return {
67
...data,

0 commit comments

Comments
 (0)