Skip to content

Commit

Permalink
feat(app, config): implement setLogLevel API
Browse files Browse the repository at this point in the history
Now you may set the firebase log level from firebase.json or via JS
This is very interesting in some cases, like when you need the AppCheck debug token
on iOS, to test AppCheck SafetyNet during development
  • Loading branch information
mikehardy committed Aug 17, 2021
1 parent fe0bd01 commit cac7be3
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/app/e2e/app.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ describe('firebase', function () {
should.equal(firebase.app().automaticDataCollectionEnabled, false);
});

it('should allow setting of log level', function () {
firebase.setLogLevel('error');
firebase.setLogLevel('verbose');
});

it('should error if logLevel is invalid', function () {
try {
firebase.setLogLevel('silent');
throw new Error('did not throw on invalid loglevel');
} catch (e) {
e.message.should.containEql('LogLevel must be one of');
}
});

it('it should initialize dynamic apps', async function () {
const appCount = firebase.apps.length;
const name = `testscoreapp${FirebaseHelpers.id}`;
Expand Down
4 changes: 4 additions & 0 deletions packages/app/firebase-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"description": "Whether automatic data collection is enabled for all products, unless overridden by product-specific data collection settings.\n Setting this to false is useful for opt-in-first data flows, for example when dealing with GDPR compliance. \nThis may be overridden dynamically in Javascript via automaticDataCollectionEnabled FirebaseAppConfig property.",
"type": "boolean"
},
"app_log_level": {
"description": "Set the log level across all modules. Only applies to iOS currently. Can be 'error', 'warn', 'info', 'debug'.\n Logs messages at the configured level or lower.\n Note that if an app is running from AppStore, it will never log above info even if level is set to a higher (more verbose) setting",
"type": "string"
},
"app_check_token_auto_refresh": {
"description": "If this flag is disabled then Firebase App Check will not periodically auto-refresh the app check token.\n This is useful for opt-in-first data flows, for example when dealing with GDPR compliance. \nIf unset it will default to the SDK-wide data collection default enabled setting. This may be overridden dynamically in Javascript.",
"type": "boolean"
Expand Down
2 changes: 2 additions & 0 deletions packages/app/ios/RNFBApp/RNFBAppModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@

@interface RNFBAppModule : NSObject <RCTBridgeModule>

- (void)setLogLevel:(NSString *)logLevel;

@end
19 changes: 19 additions & 0 deletions packages/app/ios/RNFBApp/RNFBAppModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ - (id)init {
[FIRApp registerLibrary:@"react-native-firebase" withVersion:[RNFBVersionString copy]];
});
#endif
if ([[RNFBJSON shared] contains:@"app_log_level"]) {
NSString *logLevel = [[RNFBJSON shared] getStringValue:@"app_log_level" defaultValue:@"info"];
[self setLogLevel:logLevel];
}
}

return self;
Expand Down Expand Up @@ -191,6 +195,21 @@ - (void)invalidate {
});
}

RCT_EXPORT_METHOD(setLogLevel : (NSString *)logLevel) {
int level = FIRLoggerLevelError;
if ([logLevel isEqualToString:@"verbose"]) {
level = FIRLoggerLevelDebug;
} else if ([logLevel isEqualToString:@"debug"]) {
level = FIRLoggerLevelDebug;
} else if ([logLevel isEqualToString:@"info"]) {
level = FIRLoggerLevelInfo;
} else if ([logLevel isEqualToString:@"warn"]) {
level = FIRLoggerLevelWarning;
}
DLog(@"RNFBSetLogLevel: setting level to %d from %@.", level, logLevel);
[[FIRConfiguration sharedInstance] setLoggerLevel:level];
}

RCT_EXPORT_METHOD(setAutomaticDataCollectionEnabled : (FIRApp *)firApp enabled : (BOOL)enabled) {
if (firApp) {
firApp.dataCollectionDefaultEnabled = enabled;
Expand Down
15 changes: 15 additions & 0 deletions packages/app/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export namespace ReactNativeFirebase {
readonly nativeErrorMessage: string;
}

export type LogLevelString = 'debug' | 'verbose' | 'info' | 'warn' | 'error' | 'silent';

export interface FirebaseAppOptions {
/**
* The Google App ID that is used to uniquely identify an instance of an app.
Expand Down Expand Up @@ -185,6 +187,19 @@ export namespace ReactNativeFirebase {
*/
app(name?: string): FirebaseApp;

/**
* Set the log level across all modules. Only applies to iOS currently, has no effect on Android.
* Should be one of 'error', 'warn', 'info', or 'debug'.
* Logs messages at the configured level or lower (less verbose / more important).
* Note that if an app is running from AppStore, it will never log above info even if
* level is set to a higher (more verbose) setting.
* Note that iOS is missing firebase-js-sdk log levels 'verbose' and 'silent'.
* 'verbose' if used will map to 'debug', 'silent' has no valid mapping and will return an error if used.
*
* @ios
*/
setLogLevel(logLevel: LogLevelString): void;

/**
* A (read-only) array of all the initialized Apps.
*/
Expand Down
18 changes: 17 additions & 1 deletion packages/app/lib/internal/registry/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
*
*/

import { isNull, isObject, isString, isUndefined } from '@react-native-firebase/app/lib/common';
import {
isIOS,
isNull,
isObject,
isString,
isUndefined,
} from '@react-native-firebase/app/lib/common';
import FirebaseApp from '../../FirebaseApp';
import { DEFAULT_APP_NAME } from '../constants';
import { getAppModule } from './nativeModule';
Expand Down Expand Up @@ -190,6 +196,16 @@ export function initializeApp(options = {}, configOrName) {
});
}

export function setLogLevel(logLevel) {
if (!['error', 'warn', 'info', 'debug', 'verbose'].includes(logLevel)) {
throw new Error('LogLevel must be one of "error", "warn", "info", "debug", "verbose"');
}

if (isIOS) {
getAppModule().setLogLevel(logLevel);
}
}

/**
*
*/
Expand Down
10 changes: 9 additions & 1 deletion packages/app/lib/internal/registry/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ import FirebaseApp from '../../FirebaseApp';
import SDK_VERSION from '../../version';
import { DEFAULT_APP_NAME, KNOWN_NAMESPACES } from '../constants';
import FirebaseModule from '../FirebaseModule';
import { getApp, getApps, initializeApp, setOnAppCreate, setOnAppDestroy } from './app';
import {
getApp,
getApps,
initializeApp,
setLogLevel,
setOnAppCreate,
setOnAppDestroy,
} from './app';

// firebase.X
let FIREBASE_ROOT = null;
Expand Down Expand Up @@ -231,6 +238,7 @@ export function createFirebaseRoot() {
return getApps();
},
SDK_VERSION,
setLogLevel,
};

for (let i = 0; i < KNOWN_NAMESPACES.length; i++) {
Expand Down
1 change: 1 addition & 0 deletions tests/firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"android_background_activity_names": "NotActuallyAnActivity",

"app_data_collection_default_enabled": false,
"app_log_level": "debug",

"app_check_token_auto_refresh": false,

Expand Down

0 comments on commit cac7be3

Please sign in to comment.