Skip to content

Commit

Permalink
feat: implement the log callback related feature on android (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhao900914 authored Jul 14, 2022
1 parent 8ffe277 commit 67bd273
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.amplitude.api.Amplitude;
import com.amplitude.api.AmplitudeClient;
import com.amplitude.api.AmplitudeLogCallback;
import com.amplitude.api.AmplitudeServerZone;
import com.amplitude.api.Identify;
import com.amplitude.api.Plan;
Expand All @@ -14,6 +15,7 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.Callback;

import org.json.JSONObject;
import org.json.JSONException;
Expand Down Expand Up @@ -334,6 +336,35 @@ public void setPlan(String instanceName, ReadableMap planProperties, Promise pro
}
}

@ReactMethod
public void enableLogging(String instanceName, Boolean enableLogging, Promise promise) {
AmplitudeClient client = Amplitude.getInstance(instanceName);
synchronized (client) {
client.enableLogging(enableLogging);
promise.resolve(true);
}
}

@ReactMethod
public void setLogCallback(String instanceName, Callback logCallback) {
AmplitudeClient client = Amplitude.getInstance(instanceName);
client.setLogCallback(new AmplitudeLogCallback() {
@Override
public void onError(String tag, String message) {
logCallback.invoke(tag, message);
}
});
}

@ReactMethod
public void setLogLevel(String instanceName, Integer logLevel, Promise promise) {
AmplitudeClient client = Amplitude.getInstance(instanceName);
synchronized (client) {
client.setLogLevel(logLevel);
promise.resolve(true);
}
}

private Revenue createRevenue(JSONObject properties) {
Revenue revenue = new Revenue();
try {
Expand Down
6 changes: 5 additions & 1 deletion example/src/utils/amplitude.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ const initAmplitude = (): Amplitude => {
version: '1.2.3',
versionId: 'example-version-id',
});

amplitudeInstance.enableLogging(true);
amplitudeInstance.setLogCallback((tag: string, message: string) => {
console.log(`${tag}: ${message}`);
});
amplitudeInstance.setLogLevel(2);
amplitudeInstance.addEventMiddleware((payload, next) => {
const { event, extra } = payload;
console.log(
Expand Down
6 changes: 6 additions & 0 deletions ios/AmplitudeReactNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ @interface RCT_EXTERN_MODULE(AmplitudeReactNative, NSObject)

RCT_EXTERN_METHOD(setPlan:(NSString *)instanceName planProperties:(NSDictionary *)planProperties resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(enableLogging:(NSString *)instanceName enableLogging:(BOOL *)enableLogging resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(setLogCallback:(NSString *)instanceName callback:(RCTResponseSenderBlock)callback)

RCT_EXTERN_METHOD(setLogLevel:(NSString *)instanceName logLevel:(NSInteger *)logLevel resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

@end
23 changes: 23 additions & 0 deletions ios/AmplitudeReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,29 @@ class ReactNative: NSObject {
resolve(true)
}

@objc
func enableLogging(_ instanceName: String,
enableLogging: Bool,
resolver resolve: RCTPromiseResolveBlock,
rejecter reject: RCTPromiseRejectBlock) -> Void {
// TODO: add the set log callback logic after iOS log enable logging implementation
}

@objc
func setLogCallback(_ instanceName: String,
callback: RCTResponseSenderBlock) -> Void {
// TODO: add the set log callback logic after iOS log callback implementation
}

@objc
func setLogLevel(_ instanceName: String,
logLevel: Int,
resolver resolve: RCTPromiseResolveBlock,
rejecter reject: RCTPromiseRejectBlock) -> Void {
// TODO: add the set log callback logic after iOS log level implementation
resolve(false)
}

private func createRevenue(_ userProperties: [String: Any]) -> AMPRevenue {
let revenue = AMPRevenue()
if userProperties["productId"] != nil {
Expand Down
32 changes: 32 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,38 @@ export class Amplitude {
return this;
}

/**
* Enable/disable message logging by the SDK.
*
* @param enableLogging whether to enable message logging by the SDK.
*/
enableLogging(enableLogging: boolean): Promise<boolean> {
return AmplitudeReactNative.enableLogging(this.instanceName, enableLogging);
}

/**
* Set log callback, it can help read and collect error message from sdk. The call back function like the following format
* (tag: string, message: string) => {
* //implement your own logic
* }
*
* @param callback
*/

setLogCallback(callback: Function): void {
AmplitudeReactNative.setLogCallback(this.instanceName, callback);
}

/**
* Sets the logging level. Logging messages will only appear if they are the same severity
* level or higher than the set log level.
*
* @param logLevel the log level
*/
setLogLevel(logLevel: number): Promise<boolean> {
return AmplitudeReactNative.setLogLevel(this.instanceName, logLevel);
}

// Private bridging calls
private _setLibraryName(libraryName: string): Promise<boolean> {
return AmplitudeReactNative.setLibraryName(this.instanceName, libraryName);
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export interface AmplitudeReactNativeModule {
eventUploadThreshold: number,
): Promise<boolean>;
setPlan(instanceName: string, plan: Plan): Promise<boolean>;
enableLogging(instanceName: string, enableLogging: boolean): Promise<boolean>;
setLogCallback(instanceName: string, callback: Function): void;
setLogLevel(instanceName: string, logLevel: number): Promise<boolean>;
}

/**
Expand Down

0 comments on commit 67bd273

Please sign in to comment.