Skip to content

Commit a6ccfaa

Browse files
loggerOrigin implementation
1 parent 14f576e commit a6ccfaa

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

packages/core/src/js/options.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,16 @@ export interface BaseReactNativeOptions {
316316
* @default false
317317
*/
318318
propagateTraceparent?: boolean;
319+
320+
/**
321+
* Controls the origin of the logger to be logged, it takes effect when `enableLogger` is set to true.
322+
* 'all' will log all origins.
323+
* 'JS' will log only enable Logger to capture JavaScript logs.
324+
* 'Native' will log only Native Logs..
325+
*
326+
* @default 'all'
327+
*/
328+
loggerOrigin?: 'all' | 'js' | 'native';
319329
}
320330

321331
export type SentryReplayQuality = 'low' | 'medium' | 'high';

packages/core/src/js/wrapper.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ export const NATIVE: SentryNativeWrapper = {
228228
enableNative: true,
229229
autoInitializeNativeSdk: true,
230230
...originalOptions,
231+
// Keeps original behavior of enableLogs by not setting it when not defined.
232+
...(originalOptions.enableLogs !== undefined
233+
? { enableLogs: originalOptions.enableLogs && originalOptions.loggerOrigin !== 'js' }
234+
: {}),
231235
};
232236

233237
if (!options.enableNative) {
@@ -273,7 +277,7 @@ export const NATIVE: SentryNativeWrapper = {
273277

274278
// filter out all the options that would crash native.
275279
/* eslint-disable @typescript-eslint/unbound-method,@typescript-eslint/no-unused-vars */
276-
const { beforeSend, beforeBreadcrumb, beforeSendTransaction, integrations, ignoreErrors, ...filteredOptions } =
280+
const { beforeSend, beforeBreadcrumb, beforeSendTransaction, integrations, ignoreErrors, loggerOrigin, ...filteredOptions } =
277281
options;
278282
/* eslint-enable @typescript-eslint/unbound-method,@typescript-eslint/no-unused-vars */
279283
const nativeIsReady = await RNSentry.initNativeSdk(filteredOptions);

packages/core/test/wrapper.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,44 @@ describe('Tests Native Wrapper', () => {
312312
expect(initParameter.ignoreErrorsStr).toBeUndefined();
313313
expect(initParameter.ignoreErrorsRegex).toBeUndefined();
314314
});
315+
316+
test('does not set enableLogs when option is undefined', async () => {
317+
await NATIVE.initNativeSdk({
318+
dsn: 'test',
319+
enableNative: true,
320+
autoInitializeNativeSdk: true,
321+
devServerUrl: undefined,
322+
defaultSidecarUrl: undefined,
323+
mobileReplayOptions: undefined,
324+
});
325+
326+
expect(RNSentry.initNativeSdk).toHaveBeenCalled();
327+
const initParameter = (RNSentry.initNativeSdk as jest.MockedFunction<any>).mock.calls[0][0];
328+
expect(initParameter.enableLogs).toBeUndefined();
329+
});
330+
331+
it.each([
332+
['without loggerOrigin', undefined, true],
333+
['with loggerOrigin set to Native', 'native' as const, true],
334+
['with loggerOrigin set to all', 'all' as const, true],
335+
['with loggerOrigin set to JS', 'js' as const, false],
336+
])('handles enableLogs %s', async (_description, loggerOrigin, expectedEnableLogs) => {
337+
await NATIVE.initNativeSdk({
338+
dsn: 'test',
339+
enableNative: true,
340+
autoInitializeNativeSdk: true,
341+
enableLogs: true,
342+
...(loggerOrigin !== undefined ? { loggerOrigin } : {}),
343+
devServerUrl: undefined,
344+
defaultSidecarUrl: undefined,
345+
mobileReplayOptions: undefined,
346+
});
347+
348+
expect(RNSentry.initNativeSdk).toHaveBeenCalled();
349+
const initParameter = (RNSentry.initNativeSdk as jest.MockedFunction<any>).mock.calls[0][0];
350+
expect(initParameter.enableLogs).toBe(expectedEnableLogs);
351+
expect(initParameter.loggerOrigin).toBeUndefined();
352+
});
315353
});
316354

317355
describe('sendEnvelope', () => {

0 commit comments

Comments
 (0)