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

Commit d7cd22b

Browse files
zashraf1985mjc1283
authored andcommitted
fix: Added logger for react native which uses console.warn for error (optimizely#360)
Summary: React Native App shows a full screen Red Message when console.error is called. This appears to be distracting for the developers. Added a new logger for React Native which uses console.warn to log error. Test plan: 1. Manually tested thoroughly. 2. Added unit tests
1 parent e3e2a1b commit d7cd22b

File tree

5 files changed

+559
-0
lines changed

5 files changed

+559
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* Copyright 2019, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
var logging = require('@optimizely/js-sdk-logging');
17+
var fns = require('./utils/fns');
18+
var configValidator = require('./utils/config_validator');
19+
var defaultErrorHandler = require('./plugins/error_handler');
20+
var defaultEventDispatcher = require('./plugins/event_dispatcher/index.browser');
21+
var enums = require('./utils/enums');
22+
var loggerPlugin = require('./plugins/logger/index.react_native');
23+
var Optimizely = require('./optimizely');
24+
var eventProcessorConfigValidator = require('./utils/event_processor_config_validator');
25+
26+
var logger = logging.getLogger();
27+
logging.setLogHandler(loggerPlugin.createLogger());
28+
logging.setLogLevel(logging.LogLevel.INFO);
29+
30+
var DEFAULT_EVENT_BATCH_SIZE = 10;
31+
var DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s
32+
33+
/**
34+
* Entry point into the Optimizely Javascript SDK for React Native
35+
*/
36+
module.exports = {
37+
logging: loggerPlugin,
38+
errorHandler: defaultErrorHandler,
39+
eventDispatcher: defaultEventDispatcher,
40+
enums: enums,
41+
42+
setLogger: logging.setLogHandler,
43+
setLogLevel: logging.setLogLevel,
44+
45+
/**
46+
* Creates an instance of the Optimizely class
47+
* @param {Object} config
48+
* @param {Object} config.datafile
49+
* @param {Object} config.errorHandler
50+
* @param {Object} config.eventDispatcher
51+
* @param {Object} config.logger
52+
* @param {Object} config.logLevel
53+
* @param {Object} config.userProfileService
54+
* @param {Object} config.eventBatchSize
55+
* @param {Object} config.eventFlushInterval
56+
* @return {Object} the Optimizely object
57+
*/
58+
createInstance: function(config) {
59+
try {
60+
config = config || {};
61+
62+
// TODO warn about setting per instance errorHandler / logger / logLevel
63+
if (config.errorHandler) {
64+
logging.setErrorHandler(config.errorHandler);
65+
}
66+
if (config.logger) {
67+
logging.setLogHandler(config.logger);
68+
// respect the logger's shouldLog functionality
69+
logging.setLogLevel(logging.LogLevel.NOTSET);
70+
}
71+
if (config.logLevel !== undefined) {
72+
logging.setLogLevel(config.logLevel);
73+
}
74+
75+
try {
76+
configValidator.validate(config);
77+
config.isValidInstance = true;
78+
} catch (ex) {
79+
logger.error(ex);
80+
config.isValidInstance = false;
81+
}
82+
83+
// Explicitly check for null or undefined
84+
// prettier-ignore
85+
if (config.skipJSONValidation == null) { // eslint-disable-line eqeqeq
86+
config.skipJSONValidation = true;
87+
}
88+
89+
config = fns.assignIn(
90+
{
91+
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
92+
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
93+
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
94+
},
95+
config,
96+
{
97+
eventDispatcher: config.eventDispatcher,
98+
// always get the OptimizelyLogger facade from logging
99+
logger: logger,
100+
errorHandler: logging.getErrorHandler(),
101+
}
102+
);
103+
104+
if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
105+
logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
106+
config.eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
107+
}
108+
if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
109+
logger.warn('Invalid eventFlushInterval %s, defaulting to %s', config.eventFlushInterval, DEFAULT_EVENT_FLUSH_INTERVAL);
110+
config.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
111+
}
112+
113+
var optimizely = new Optimizely(config);
114+
115+
return optimizely;
116+
} catch (e) {
117+
logger.error(e);
118+
return null;
119+
}
120+
},
121+
};

0 commit comments

Comments
 (0)