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

Commit 5d97c0c

Browse files
committed
Added logger for react native which uses console.warn for error
1 parent e3e2a1b commit 5d97c0c

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

packages/logging/src/logger.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export class ConsoleLogHandler implements LogHandler {
173173
* @param {string[]} logArguments
174174
* @memberof ConsoleLogger
175175
*/
176-
private consoleLog(logLevel: LogLevel, logArguments: [string, ...string[]]) {
176+
protected consoleLog(logLevel: LogLevel, logArguments: [string, ...string[]]) {
177177
switch (logLevel) {
178178
case LogLevel.DEBUG:
179179
console.log.apply(console, logArguments)
@@ -193,6 +193,22 @@ export class ConsoleLogHandler implements LogHandler {
193193
}
194194
}
195195

196+
export class ReactNativeConsoleLogHandler extends ConsoleLogHandler {
197+
/**
198+
* @private
199+
* @param {LogLevel} logLevel
200+
* @param {string[]} logArguments
201+
* @memberof ConsoleLogger
202+
*/
203+
protected consoleLog(logLevel: LogLevel, logArguments: [string, ...string[]]) {
204+
if (logLevel === LogLevel.ERROR) {
205+
console.warn.apply(console, logArguments);
206+
} else {
207+
super.consoleLog(logLevel, logArguments);
208+
}
209+
}
210+
}
211+
196212
let globalLogLevel: LogLevel = LogLevel.NOTSET
197213
let globalLogHandler: LogHandler | null = null
198214

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2016-2017, 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 browserIndex = require('./index.browser');
18+
19+
/**
20+
* Entry point into the Optimizely Browser SDK for React Native Apps
21+
*/
22+
module.exports = {
23+
...browserIndex,
24+
25+
/**
26+
* Creates an instance of the Optimizely class
27+
* @param {Object} config
28+
* @param {Object} config.datafile
29+
* @param {Object} config.errorHandler
30+
* @param {Object} config.eventDispatcher
31+
* @param {Object} config.logger
32+
* @param {Object} config.logLevel
33+
* @param {Object} config.userProfileService
34+
* @param {Object} config.eventBatchSize
35+
* @param {Object} config.eventFlushInterval
36+
* @return {Object} the Optimizely object
37+
*/
38+
createInstance: function(config) {
39+
return browserIndex.createInstance({
40+
logger: new logging.ReactNativeConsoleLogHandler(),
41+
...config,
42+
})
43+
}
44+
};

packages/optimizely-sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "JavaScript SDK for Optimizely X Full Stack",
55
"main": "lib/index.node.js",
66
"browser": "lib/index.browser.js",
7+
"react-native": "lib/index.react_native.js",
78
"typings": "lib/index.d.ts",
89
"scripts": {
910
"test": "mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js --recursive --exit --require lib/tests/exit_on_unhandled_rejection.js",

0 commit comments

Comments
 (0)