Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: logging feature in android native module #319

Merged
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import io.customer.reactnative.sdk.constant.Keys
import io.customer.reactnative.sdk.extension.toMap
import io.customer.reactnative.sdk.logging.CustomerIOReactNativeLoggingWrapper
import io.customer.reactnative.sdk.messagingpush.RNCIOPushMessaging
import io.customer.sdk.CustomerIO
import io.customer.sdk.CustomerIOBuilder
import io.customer.sdk.core.di.SDKComponent
import io.customer.sdk.core.util.CioLogLevel
import io.customer.sdk.core.util.Logger
import io.customer.sdk.data.model.Region


class NativeCustomerIOModule(
reactContext: ReactApplicationContext,
private val reactContext: ReactApplicationContext,
private val pushMessagingModule: RNCIOPushMessaging,
private val inAppMessagingModule: RNCIOInAppMessaging,
) : ReactContextBaseJavaModule(reactContext) {
private val logger: Logger
get() = SDKComponent.logger
private lateinit var logger: Logger

// If the SDK is not initialized, `CustomerIO.instance()` throws an exception
private val customerIOInstance: CustomerIO?
Expand All @@ -45,6 +44,8 @@ class NativeCustomerIOModule(
fun initialize(
configJson: ReadableMap,
logLevel: String) {

logger = CustomerIOReactNativeLoggingWrapper.getInstance(reactContext, CioLogLevel.getLogLevel(logLevel))
val packageConfig = configJson.toMap()
val cdpApiKey = packageConfig[Keys.Config.CDP_API_KEY]
try {
Expand Down Expand Up @@ -137,4 +138,4 @@ class NativeCustomerIOModule(
companion object {
internal const val MODULE_NAME = "NativeCustomerIO"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager
import io.customer.reactnative.sdk.logging.CustomerIOReactNativeLoggingEmitter
import io.customer.reactnative.sdk.messagingpush.RNCIOPushMessaging

class CustomerIOReactNativePackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
val pushMessagingModule = RNCIOPushMessaging(reactContext)
val inAppMessagingModule = RNCIOInAppMessaging(reactContext)
val loggerModule = CustomerIOReactNativeLoggingEmitter(reactContext)
return listOf(
inAppMessagingModule,
pushMessagingModule,
loggerModule,
NativeCustomerIOModule(
reactContext = reactContext,
pushMessagingModule = pushMessagingModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.customer.reactnative.sdk.logging
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.WritableMap
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.modules.core.DeviceEventManagerModule
import io.customer.sdk.core.util.CioLogLevel
import kotlin.collections.buildMap
import io.customer.sdk.core.di.SDKComponent
import io.customer.sdk.core.util.Logger

@ReactModule(name = CustomerIOReactNativeLoggingEmitter.NAME)
class CustomerIOReactNativeLoggingEmitter(
private val reactContext: ReactApplicationContext,
) : ReactContextBaseJavaModule(reactContext) {
companion object {
const val EVENT_NAME = "CioLogEvent"
const val NAME = "CioLoggingEmitter"

}
private var listenerCount = 0

override fun getName(): String {
return NAME
}

@ReactMethod
fun addListener(eventName: String) {
listenerCount++
}

@ReactMethod
fun removeListeners(count: Int) {
listenerCount -= count
}

fun sendEvent(params: WritableMap) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(EVENT_NAME, params)
}
}

class CustomerIOReactNativeLoggingWrapper private constructor(
private var moduleRegistry: ReactApplicationContext,
override var logLevel: CioLogLevel): Logger {

private val emitter: CustomerIOReactNativeLoggingEmitter?
get() = moduleRegistry?.getNativeModule(CustomerIOReactNativeLoggingEmitter::class.java)

companion object {
fun getInstance(moduleRegistry: ReactApplicationContext, logLevel: CioLogLevel): CustomerIOReactNativeLoggingWrapper {
val instance = CustomerIOReactNativeLoggingWrapper(moduleRegistry, logLevel)
SDKComponent.overrideDependency<Logger>(instance)
return instance
}
ami-aman marked this conversation as resolved.
Show resolved Hide resolved
}

override fun debug(message: String) {
emit(message, CioLogLevel.DEBUG)
}
ami-aman marked this conversation as resolved.
Show resolved Hide resolved

override fun info(message: String) {
emit(message, CioLogLevel.INFO)
}

override fun error(message: String) {
emit(message, CioLogLevel.ERROR)
}

private fun emit(message: String, level: CioLogLevel) {
if (shouldEmit(level)) {
val data = buildMap {
put("logLevel", level.name.lowercase())
put("message", message)
}
emitter?.sendEvent(Arguments.makeNativeMap(data))
}
}

private fun shouldEmit(level: CioLogLevel): Boolean {
return when (logLevel) {
CioLogLevel.NONE -> false
CioLogLevel.ERROR -> level == CioLogLevel.ERROR
CioLogLevel.INFO -> level == CioLogLevel.ERROR || level == CioLogLevel.INFO
CioLogLevel.DEBUG -> true
}
}
}
Loading