Skip to content

Commit

Permalink
[MOB-11440] Add debugLogsLevel to InstabugConfig (#873)
Browse files Browse the repository at this point in the history
After adding Instabug.init API that takes an extensible InstabugConfig, this PR does the following:
1. Adds debugLogsLevel optional property to InstabugConfig, and a new enum LogLevel which controls the verbosity of SDK logs. This works on both platforms. 
2. Deprecates the old debug logs APIs and enums.
  • Loading branch information
DavidMina96 authored and ymabdallah committed Feb 20, 2023
1 parent e4b8c7a commit c1807cb
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Unreleased

- Deprecates Instabug.start in favour of Instabug.init that takes a configuration object for SDK initialization.
- Deprecates Instabug.setDebugEnabled, Instabug.setSdkDebugLogsLevel, and APM.setLogLevel in favour of debugLogsLevel property, which can be passed to InstabugConfig while initializing the SDK using Instabug.init.
- Deprecates the enums: sdkDebugLogsLevel and logLevel in favour of a new enum LogLevel.

## 11.6.0 (2022-12-29)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static Map<String, Object> getAll() {
putAll(actionTypes);
putAll(extendedBugReportStates);
putAll(reproStates);
putAll(sdkLogLevels);
putAll(promptOptions);
putAll(locales);
putAll(placeholders);
Expand Down Expand Up @@ -145,6 +146,13 @@ static Map<String, Object> getAll() {
put("reproStepsDisabled", State.DISABLED);
}};

static final ArgsMap<Integer> sdkLogLevels = new ArgsMap<Integer>() {{
put("sdkDebugLogsLevelNone", com.instabug.library.LogLevel.NONE);
put("sdkDebugLogsLevelError", com.instabug.library.LogLevel.ERROR);
put("sdkDebugLogsLevelDebug", com.instabug.library.LogLevel.DEBUG);
put("sdkDebugLogsLevelVerbose", com.instabug.library.LogLevel.VERBOSE);
}};

@Deprecated
static final ArgsMap<Integer> promptOptions = new ArgsMap<Integer>() {{
put("promptOptionBug", PluginPromptOption.PromptOptionIdentifier.BUG_REPORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.instabug.library.InstabugState;
import com.instabug.library.OnSdkDismissCallback;
import com.instabug.library.Platform;
import com.instabug.library.LogLevel;
import com.instabug.library.extendedbugreport.ExtendedBugReport;
import com.instabug.library.internal.module.InstabugLocale;
import com.instabug.library.invocation.InstabugInvocationEvent;
Expand Down Expand Up @@ -133,19 +134,21 @@ public void run() {
* @param invocationEventValues The events that invoke the SDK's UI.
*/
@ReactMethod
public void init(final String token, final ReadableArray invocationEventValues) {
public void init(final String token, final ReadableArray invocationEventValues, final String logLevel) {
MainThreadHandler.runOnMainThread(new Runnable() {
@Override
public void run() {
try {
final ArrayList<String> keys = ArrayUtil.parseReadableArrayOfStrings(invocationEventValues);
final ArrayList<InstabugInvocationEvent> parsedInvocationEvents = ArgsRegistry.invocationEvents.getAll(keys);
final int parsedLogLevel = ArgsRegistry.sdkLogLevels.getOrDefault(logLevel, LogLevel.ERROR);

setCurrentPlatform();
setBaseUrlForDeprecationLogs();

new Instabug.Builder(getCurrentActivity().getApplication(), token)
.setInvocationEvents(parsedInvocationEvents.toArray(new InstabugInvocationEvent[0]))
.setSdkDebugLogsLevel(parsedLogLevel)
.build();

// Temporarily disabling APM hot launches
Expand Down
4 changes: 3 additions & 1 deletion example/ios/InstabugSampleTests/InstabugSampleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ - (void)testInit {
IBGInvocationEvent floatingButtonInvocationEvent = IBGInvocationEventFloatingButton;
NSString *appToken = @"app_token";
NSArray *invocationEvents = [NSArray arrayWithObjects:[NSNumber numberWithInteger:floatingButtonInvocationEvent], nil];
IBGSDKDebugLogsLevel sdkDebugLogsLevel = IBGSDKDebugLogsLevelDebug;

XCTestExpectation *expectation = [self expectationWithDescription:@"Testing [Instabug init]"];

OCMStub([mock startWithToken:appToken invocationEvents:floatingButtonInvocationEvent]);
[self.instabugBridge init:appToken invocationEvents:invocationEvents];
[self.instabugBridge init:appToken invocationEvents:invocationEvents debugLogsLevel:sdkDebugLogsLevel];

[[NSRunLoop mainRunLoop] performBlock:^{
OCMVerify([mock startWithToken:appToken invocationEvents:floatingButtonInvocationEvent]);
Expand Down
2 changes: 1 addition & 1 deletion ios/RNInstabug/InstabugReactBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

- (void)setEnabled:(BOOL)isEnabled;

- (void)init:(NSString *)token invocationEvents:(NSArray *)invocationEventsArray;
- (void)init:(NSString *)token invocationEvents:(NSArray *)invocationEventsArray debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel;

- (void)setUserData:(NSString *)userData;

Expand Down
5 changes: 3 additions & 2 deletions ios/RNInstabug/InstabugReactBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ - (dispatch_queue_t)methodQueue {
Instabug.enabled = isEnabled;
}

RCT_EXPORT_METHOD(init:(NSString *)token invocationEvents:(NSArray*)invocationEventsArray) {
RCT_EXPORT_METHOD(init:(NSString *)token invocationEvents:(NSArray*)invocationEventsArray debugLogsLevel:(IBGSDKDebugLogsLevel)sdkDebugLogsLevel) {
SEL setPrivateApiSEL = NSSelectorFromString(@"setCurrentPlatform:");
if ([[Instabug class] respondsToSelector:setPrivateApiSEL]) {
NSInteger *platform = IBGPlatformReactNative;
Expand All @@ -57,7 +57,8 @@ - (dispatch_queue_t)methodQueue {
invocationEvents |= [boxedValue intValue];
}
[Instabug startWithToken:token invocationEvents:invocationEvents];

[Instabug setSdkDebugLogsLevel:sdkDebugLogsLevel];

RCTAddLogFunction(InstabugReactLogFunction);
RCTSetLogThreshold(RCTLogLevelInfo);

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export {
Replies,
Surveys,
};
export * from './utils/Enums';

export type { InstabugConfig };
export type { Survey };
Expand Down
11 changes: 11 additions & 0 deletions src/models/InstabugConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import type { invocationEvent } from '../utils/ArgsRegistry';
import type { LogLevel } from '../utils/Enums';

export interface InstabugConfig {
/**
* The token that identifies the app. You can find it on your dashboard.
*/
token: string;
/**
* An array of events that invoke the SDK's UI.
*/
invocationEvents: invocationEvent[];
/**
* An optional LogLevel to indicate the verbosity of SDK logs. Default is Error.
*/
debugLogsLevel?: LogLevel;
}
2 changes: 2 additions & 0 deletions src/modules/APM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { logLevel } from '../utils/ArgsRegistry';
export { logLevel };

/**
* @deprecated Pass a LogLevel to debugLogsLevel in Instabug.init instead.
*
* Sets the printed logs priority. Filter to one of the following levels:
*
* - `logLevelNone` disables all APM SDK console logs.
Expand Down
14 changes: 11 additions & 3 deletions src/modules/Instabug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
strings,
welcomeMessageMode,
} from '../utils/ArgsRegistry';
import { LogLevel } from '../utils/Enums';
import IBGEventEmitter from '../utils/IBGEventEmitter';
import InstabugConstants from '../utils/InstabugConstants';
import InstabugUtils, { stringifyIfNotString } from '../utils/InstabugUtils';
Expand Down Expand Up @@ -73,14 +74,17 @@ export const start = (token: string, invocationEvents: invocationEvent[]) => {
* This is the main SDK method that does all the magic. This is the only
* method that SHOULD be called.
* Should be called in constructor of the AppRegistry component
* @param token The token that identifies the app. You can find it on your dashboard.
* @param invocationEvents The events that invoke the SDK's UI.
* @param config SDK configurations. See {@link InstabugConfig} for more info.
*/
export const init = (config: InstabugConfig) => {
InstabugUtils.captureJsErrors();
NetworkLogger.setEnabled(true);

NativeInstabug.init(config.token, config.invocationEvents);
NativeInstabug.init(
config.token,
config.invocationEvents,
config.debugLogsLevel ?? LogLevel.Error,
);

_isFirstScreen = true;
_currentScreen = firstScreen;
Expand Down Expand Up @@ -137,6 +141,8 @@ export const setSessionProfilerEnabled = (isEnabled: boolean) => {
};

/**
* @deprecated Pass a {@link LogLevel} to debugLogsLevel in {@link init} instead. This will work on both Android and iOS.
*
* This API sets the verbosity level of logs used to debug The SDK. The default value in debug
* mode is sdkDebugLogsLevelVerbose and in production is sdkDebugLogsLevelError.
* @param level The verbosity level of logs.
Expand Down Expand Up @@ -400,6 +406,8 @@ export const clearAllUserAttributes = () => {
};

/**
* @deprecated Pass a {@link LogLevel} to debugLogsLevel in {@link init} instead. This will work on both Android and iOS.
*
* Enable/Disable debug logs from Instabug SDK
* Default state: disabled
*
Expand Down
4 changes: 4 additions & 0 deletions src/utils/ArgsRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { NativeInstabug } from '../native';

/**
* @deprecated Pass a `LogLevel` to `debugLogsLevel` in `Instabug.init` instead.
*
* Verbosity level of the SDK debug logs. This has nothing to do with IBGLog,
* and only affect the logs used to debug the SDK itself.
*/
Expand All @@ -12,6 +14,8 @@ export enum sdkDebugLogsLevel {
}

/**
* @deprecated Pass a `LogLevel` to `debugLogsLevel` in `Instabug.init` instead.
*
* APM Log Level.
*/
export enum logLevel {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/Enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NativeInstabug } from '../native';

export enum LogLevel {
Verbose = NativeInstabug.sdkDebugLogsLevelVerbose,
Debug = NativeInstabug.sdkDebugLogsLevelDebug,
Error = NativeInstabug.sdkDebugLogsLevelError,
None = NativeInstabug.sdkDebugLogsLevelNone,
}
3 changes: 3 additions & 0 deletions tests/modules/Instabug.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import waitForExpect from 'wait-for-expect';

import Report from '../../src/models/Report';
import * as Instabug from '../../src/modules/Instabug';
import { LogLevel } from '../../src/utils/Enums';
import IBGEventEmitter from '../../src/utils/IBGEventEmitter';
import IBGConstants from '../../src/utils/InstabugConstants';
import InstabugUtils from '../../src/utils/InstabugUtils';
Expand Down Expand Up @@ -140,13 +141,15 @@ describe('Instabug Module', () => {
const instabugConfig = {
token: 'some-token',
invocationEvents: [Instabug.invocationEvent.floatingButton, Instabug.invocationEvent.shake],
debugLogsLevel: LogLevel.Debug,
};
Instabug.init(instabugConfig);

expect(NativeInstabug.init).toBeCalledTimes(1);
expect(NativeInstabug.init).toBeCalledWith(
instabugConfig.token,
instabugConfig.invocationEvents,
instabugConfig.debugLogsLevel,
);
});

Expand Down

0 comments on commit c1807cb

Please sign in to comment.