Skip to content

Commit 9f55720

Browse files
authored
Merge pull request #360 from Iterable/jay/MOB-2572-logger
[MOB-2572] create a logger class and have option to disable logging
2 parents a5ec713 + 8f79883 commit 9f55720

7 files changed

+110
-42
lines changed

ts/Iterable.ts

+56-32
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import IterableInAppManager from './IterableInAppManager'
1717
import IterableInAppMessage from './IterableInAppMessage'
1818
import IterableConfig from './IterableConfig'
19+
import { IterableLogger } from './IterableLogger'
1920

2021
const RNIterableAPI = NativeModules.RNIterableAPI
2122
const RNEventEmitter = new NativeEventEmitter(RNIterableAPI)
@@ -121,15 +122,10 @@ enum EventName {
121122
}
122123

123124
class Iterable {
124-
/**
125-
* inAppManager instance
126-
*/
127125
static inAppManager = new IterableInAppManager()
128126

127+
static logger: IterableLogger
129128

130-
/**
131-
* savedConfig instance.
132-
*/
133129
static savedConfig: IterableConfig
134130

135131
/**
@@ -138,9 +134,12 @@ class Iterable {
138134
* @param {IterableConfig} config
139135
*/
140136
static initialize(apiKey: string, config: IterableConfig = new IterableConfig()): Promise<boolean> {
141-
console.log("initialize: " + apiKey);
142-
143137
Iterable.savedConfig = config
138+
139+
Iterable.logger = new IterableLogger(Iterable.savedConfig)
140+
141+
Iterable.logger.log("initialize: " + apiKey)
142+
144143
this.setupEventHandlers()
145144
const version = this.getVersionFromPackageJson()
146145

@@ -152,9 +151,12 @@ class Iterable {
152151
* This method is used internally to connect to staging environment.
153152
*/
154153
static initialize2(apiKey: string, config: IterableConfig = new IterableConfig(), apiEndPoint: string): Promise<boolean> {
155-
console.log("initialize2: " + apiKey);
156-
157154
Iterable.savedConfig = config
155+
156+
Iterable.logger = new IterableLogger(Iterable.savedConfig)
157+
158+
Iterable.logger.log("initialize2: " + apiKey);
159+
158160
this.setupEventHandlers()
159161
const version = this.getVersionFromPackageJson()
160162

@@ -166,15 +168,17 @@ class Iterable {
166168
* @param {string | undefined} email the email address of the user
167169
*/
168170
static setEmail(email: string | undefined) {
169-
console.log("setEmail: " + email)
171+
Iterable.logger.log("setEmail: " + email)
172+
170173
RNIterableAPI.setEmail(email)
171174
}
172175

173176
/**
174177
* Get the email of the current user
175178
*/
176179
static getEmail(): Promise<string | undefined> {
177-
console.log("getEmail")
180+
Iterable.logger.log("getEmail")
181+
178182
return RNIterableAPI.getEmail()
179183
}
180184

@@ -183,39 +187,44 @@ class Iterable {
183187
* @param {string | undefined} userId the ID of the user
184188
*/
185189
static setUserId(userId: string | undefined) {
186-
console.log("setUserId: " + userId)
190+
Iterable.logger.log("setUserId: " + userId)
191+
187192
RNIterableAPI.setUserId(userId)
188193
}
189194

190195
/**
191196
* Get the user ID of the current user
192197
*/
193198
static getUserId(): Promise<string | undefined> {
194-
console.log("getUserId")
199+
Iterable.logger.log("getUserId")
200+
195201
return RNIterableAPI.getUserId()
196202
}
197203

198204
/**
199205
*
200206
*/
201207
static disableDeviceForCurrentUser() {
202-
console.log("disableDeviceForCurrentUser")
208+
Iterable.logger.log("disableDeviceForCurrentUser")
209+
203210
RNIterableAPI.disableDeviceForCurrentUser()
204211
}
205212

206213
/**
207214
*
208215
*/
209216
static getLastPushPayload(): Promise<any | undefined> {
210-
console.log("getLastPushPayload")
217+
Iterable.logger.log("getLastPushPayload")
218+
211219
return RNIterableAPI.getLastPushPayload()
212220
}
213221

214222
/**
215223
*
216224
*/
217225
static getAttributionInfo(): Promise<IterableAttributionInfo | undefined> {
218-
console.log("getAttributionInfo")
226+
Iterable.logger.log("getAttributionInfo")
227+
219228
return RNIterableAPI.getAttributionInfo().then((dict: any | undefined) => {
220229
if (dict) {
221230
return new IterableAttributionInfo(dict["campaignId"] as number, dict["templateId"] as number, dict["messageId"] as string)
@@ -231,7 +240,8 @@ class Iterable {
231240
* @param {attributionInfo} IterableAttributionInfo
232241
*/
233242
static setAttributionInfo(attributionInfo?: IterableAttributionInfo) {
234-
console.log("setAttributionInfo")
243+
Iterable.logger.log("setAttributionInfo")
244+
235245
RNIterableAPI.setAttributionInfo(attributionInfo)
236246
}
237247

@@ -244,7 +254,8 @@ class Iterable {
244254
* @param {any | undefined} dataFields
245255
*/
246256
static trackPushOpenWithCampaignId(campaignId: number, templateId: number, messageId: string | undefined, appAlreadyRunning: boolean, dataFields: any | undefined) {
247-
console.log("trackPushOpenWithCampaignId")
257+
Iterable.logger.log("trackPushOpenWithCampaignId")
258+
248259
RNIterableAPI.trackPushOpenWithCampaignId(campaignId, templateId, messageId, appAlreadyRunning, dataFields)
249260
}
250261

@@ -253,13 +264,15 @@ class Iterable {
253264
* @param {Array<IterableCommerceItem>} items
254265
*/
255266
static updateCart(items: Array<IterableCommerceItem>) {
256-
console.log("updateCart")
267+
Iterable.logger.log("updateCart")
268+
257269
RNIterableAPI.updateCart(items)
258270
}
259271

260272
static wakeApp() {
261273
if (Platform.OS === "android") {
262-
console.log('Attempting to wake the app')
274+
Iterable.logger.log("Attempting to wake the app")
275+
263276
RNIterableAPI.wakeApp();
264277
}
265278
}
@@ -271,7 +284,8 @@ class Iterable {
271284
* @param {any | undefined} dataFields
272285
*/
273286
static trackPurchase(total: number, items: Array<IterableCommerceItem>, dataFields: any | undefined) {
274-
console.log("trackPurchase")
287+
Iterable.logger.log("trackPurchase")
288+
275289
RNIterableAPI.trackPurchase(total, items, dataFields)
276290
}
277291

@@ -281,7 +295,8 @@ class Iterable {
281295
* @param {IterableInAppLocation} location
282296
*/
283297
static trackInAppOpen(message: IterableInAppMessage, location: IterableInAppLocation) {
284-
console.log("trackInAppOpen")
298+
Iterable.logger.log("trackInAppOpen")
299+
285300
RNIterableAPI.trackInAppOpen(message.messageId, location)
286301
}
287302

@@ -292,7 +307,8 @@ class Iterable {
292307
* @param {string} clickedUrl
293308
*/
294309
static trackInAppClick(message: IterableInAppMessage, location: IterableInAppLocation, clickedUrl: string) {
295-
console.log("trackInAppClick")
310+
Iterable.logger.log("trackInAppClick")
311+
296312
RNIterableAPI.trackInAppClick(message.messageId, location, clickedUrl)
297313
}
298314

@@ -304,7 +320,8 @@ class Iterable {
304320
* @param {string} clickedUrl
305321
*/
306322
static trackInAppClose(message: IterableInAppMessage, location: IterableInAppLocation, source: IterableInAppCloseSource, clickedUrl?: string | undefined) {
307-
console.log("trackInAppClose")
323+
Iterable.logger.log("trackInAppClose")
324+
308325
RNIterableAPI.trackInAppClose(message.messageId, location, source, clickedUrl)
309326
}
310327

@@ -315,7 +332,8 @@ class Iterable {
315332
* @param {IterableInAppDeleteSource} source
316333
*/
317334
static inAppConsume(message: IterableInAppMessage, location: IterableInAppLocation, source: IterableInAppDeleteSource) {
318-
console.log("inAppConsume")
335+
Iterable.logger.log("inAppConsume")
336+
319337
RNIterableAPI.inAppConsume(message.messageId, location, source)
320338
}
321339

@@ -325,7 +343,8 @@ class Iterable {
325343
* @param {any | undefined} dataFields
326344
*/
327345
static trackEvent(name: string, dataFields: any | undefined) {
328-
console.log("trackEvent")
346+
Iterable.logger.log("trackEvent")
347+
329348
RNIterableAPI.trackEvent(name, dataFields)
330349
}
331350

@@ -335,7 +354,8 @@ class Iterable {
335354
* @param {boolean} mergeNestedObjects Whether to merge top level objects instead of overwriting
336355
*/
337356
static updateUser(dataFields: any, mergeNestedObjects: boolean) {
338-
console.log("updateUser")
357+
Iterable.logger.log("updateUser")
358+
339359
RNIterableAPI.updateUser(dataFields, mergeNestedObjects)
340360
}
341361

@@ -344,7 +364,8 @@ class Iterable {
344364
* @param email the new email to set
345365
*/
346366
static updateEmail(email: string) {
347-
console.log("updateEmail")
367+
Iterable.logger.log("updateEmail")
368+
348369
RNIterableAPI.updateEmail(email)
349370
}
350371

@@ -353,7 +374,8 @@ class Iterable {
353374
* @param {string} link URL in string form to be either opened as an app link or as a normal one
354375
*/
355376
static handleAppLink(link: string): Promise<boolean> {
356-
console.log("handleAppLink")
377+
Iterable.logger.log("handleAppLink")
378+
357379
return RNIterableAPI.handleAppLink(link)
358380
}
359381

@@ -372,7 +394,8 @@ class Iterable {
372394
subscribedMessageTypeIds: Array<number> | undefined,
373395
campaignId: number,
374396
templateId: number) {
375-
console.log("updateSubscriptions")
397+
Iterable.logger.log("updateSubscriptions")
398+
376399
RNIterableAPI.updateSubscriptions(emailListIds, unsubscribedChannelIds, unsubscribedMessageTypeIds, subscribedMessageTypeIds, campaignId, templateId)
377400
}
378401

@@ -391,6 +414,7 @@ class Iterable {
391414
const url = dict["url"]
392415
const context = IterableActionContext.fromDict(dict["context"])
393416
Iterable.wakeApp()
417+
394418
if (Platform.OS === "android") {
395419
//Give enough time for Activity to wake up.
396420
setTimeout(() => {
@@ -443,7 +467,7 @@ class Iterable {
443467
.then(canOpen => {
444468
if (canOpen) { Linking.openURL(url) }
445469
})
446-
.catch(reason => { console.log("could not open url: " + reason) })
470+
.catch(reason => { Iterable.logger.log("could not open url: " + reason) })
447471
}
448472
}
449473
}

ts/IterableConfig.ts

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class IterableConfig {
6565
*/
6666
logLevel: IterableLogLevel = IterableLogLevel.info
6767

68+
/**
69+
* Set whether the React Native SDK should print function calls to console
70+
* This is for calls within the React Native layer, and is separate from `logLevel`
71+
* which affects the Android and iOS native SDKs
72+
*/
73+
logReactNativeSdkCalls: boolean = true
74+
6875
/**
6976
* Set the amount of time (in seconds) before the current auth token expires to make a call to retrieve a new one
7077
*/

ts/IterableInAppManager.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
IterableInAppLocation,
88
IterableInAppDeleteSource,
99
} from '.'
10+
import { Iterable } from './Iterable'
1011

1112
import IterableInAppMessage from './IterableInAppMessage'
1213

@@ -17,15 +18,17 @@ class IterableInAppManager {
1718
* Returns a list of all in-app messages.
1819
*/
1920
getMessages(): Promise<Array<IterableInAppMessage>> {
20-
console.log("InAppManager.getMessages")
21+
Iterable.logger.log("InAppManager.getMessages")
22+
2123
return RNIterableAPI.getInAppMessages().then((messages: Array<any>) => messages.map(message => { return IterableInAppMessage.fromDict(message) }))
2224
}
2325

2426
/**
2527
* Returns a list of all in-app messages that are marked with `saveToInbox`
2628
*/
2729
getInboxMessages(): Promise<Array<IterableInAppMessage>> {
28-
console.log("InAppManager.getInboxMessages")
30+
Iterable.logger.log("InAppManager.getInboxMessages")
31+
2932
return RNIterableAPI.getInboxMessages().then((messages: Array<any>) => messages.map(message => { return IterableInAppMessage.fromDict(message) }))
3033
}
3134

@@ -35,7 +38,8 @@ class IterableInAppManager {
3538
* @param {boolean} consume Set to true to consume the event from the server queue if the message is shown. This should be default.
3639
*/
3740
showMessage(message: IterableInAppMessage, consume: boolean): Promise<string | undefined> {
38-
console.log("InAppManager.show")
41+
Iterable.logger.log("InAppManager.show")
42+
3943
return RNIterableAPI.showMessage(message.messageId, consume)
4044
}
4145

@@ -46,7 +50,8 @@ class IterableInAppManager {
4650
* @param {IterableInAppDeleteSource} source
4751
*/
4852
removeMessage(message: IterableInAppMessage, location: IterableInAppLocation, source: IterableInAppDeleteSource): void {
49-
console.log("InAppManager.remove")
53+
Iterable.logger.log("InAppManager.remove")
54+
5055
return RNIterableAPI.removeMessage(message.messageId, location, source)
5156
}
5257

@@ -56,7 +61,8 @@ class IterableInAppManager {
5661
* @param {boolean} read
5762
*/
5863
setReadForMessage(message: IterableInAppMessage, read: boolean) {
59-
console.log("InAppManager.setRead")
64+
Iterable.logger.log("InAppManager.setRead")
65+
6066
RNIterableAPI.setReadForMessage(message.messageId, read)
6167
}
6268

@@ -65,7 +71,8 @@ class IterableInAppManager {
6571
* @param {IterableInAppMessage} message
6672
*/
6773
getHtmlContentForMessage(message: IterableInAppMessage): Promise<IterableHtmlInAppContent> {
68-
console.log("InAppManager.getHtmlContentForMessage")
74+
Iterable.logger.log("InAppManager.getHtmlContentForMessage")
75+
6976
return RNIterableAPI.getHtmlInAppContentForMessage(message.messageId)
7077
.then((content: any) => {
7178
return IterableHtmlInAppContent.fromDict(content)
@@ -77,7 +84,8 @@ class IterableInAppManager {
7784
* @param {boolean} paused whether the automatic displaying should be paused
7885
*/
7986
setAutoDisplayPaused(paused: boolean) {
80-
console.log("InAppManager.setAutoDisplayPaused")
87+
Iterable.logger.log("InAppManager.setAutoDisplayPaused")
88+
8189
RNIterableAPI.setAutoDisplayPaused(paused)
8290
}
8391
}

0 commit comments

Comments
 (0)