From 8b1f7b2bb5fe1243b48cb3985500efe00573d31a Mon Sep 17 00:00:00 2001 From: IljaDaderko Date: Wed, 28 Aug 2019 15:47:50 +0300 Subject: [PATCH 1/9] logger barebones --- packages/app/lib/internal/registry/nativeModule.js | 5 +++++ packages/app/lib/utils/index.js | 9 +++++++++ packages/app/lib/utils/logger.js | 13 +++++++++++++ tests/app.js | 2 ++ 4 files changed, 29 insertions(+) create mode 100644 packages/app/lib/utils/logger.js diff --git a/packages/app/lib/internal/registry/nativeModule.js b/packages/app/lib/internal/registry/nativeModule.js index 03f001ca08..ca0517d8c1 100644 --- a/packages/app/lib/internal/registry/nativeModule.js +++ b/packages/app/lib/internal/registry/nativeModule.js @@ -16,6 +16,7 @@ */ import { NativeModules, Platform } from 'react-native'; +import Logger from '../../utils/logger'; import { APP_NATIVE_MODULE } from '../constants'; import NativeFirebaseError from '../NativeFirebaseError'; import RNFBNativeEventEmitter from '../RNFBNativeEventEmitter'; @@ -39,6 +40,10 @@ function nativeModuleKey(module) { */ function nativeModuleMethodWrapped(namespace, method, argToPrepend) { return (...args) => { + if (Logger.config.enableMethodLogging) { + Logger.info(`${namespace} - ${method.name} - ${JSON.stringify(args)}`); + } + const possiblePromise = method(...[...argToPrepend, ...args]); if (possiblePromise && possiblePromise.then) { diff --git a/packages/app/lib/utils/index.js b/packages/app/lib/utils/index.js index 5a5631146b..cfe61ec5f5 100644 --- a/packages/app/lib/utils/index.js +++ b/packages/app/lib/utils/index.js @@ -17,6 +17,7 @@ import { isIOS } from '../../lib/common'; import { createModuleNamespace, FirebaseModule } from '../../lib/internal'; +import Logger from './logger'; import UtilsStatics from './UtilsStatics'; const namespace = 'utils'; @@ -30,6 +31,14 @@ class FirebaseUtilsModule extends FirebaseModule { } return this.native.isRunningInTestLab; } + + enableLogger(config) { + Logger.config = { ...Logger.config, ...config }; + } + + logger() { + return Logger; + } } // import { utils } from '@react-native-firebase/app'; diff --git a/packages/app/lib/utils/logger.js b/packages/app/lib/utils/logger.js new file mode 100644 index 0000000000..2e053df9b7 --- /dev/null +++ b/packages/app/lib/utils/logger.js @@ -0,0 +1,13 @@ +const config = { + enableMethodLogging: false, + enableEventLogging: false, +}; + +function info(text) { + console.log(text); +} + +export default { + config, + info, +}; diff --git a/tests/app.js b/tests/app.js index 7011a15110..6be40e57fa 100755 --- a/tests/app.js +++ b/tests/app.js @@ -44,6 +44,8 @@ jet.exposeContextProperty('NativeModules', NativeModules); jet.exposeContextProperty('NativeEventEmitter', NativeEventEmitter); jet.exposeContextProperty('module', firebase); +firebase.utils().enableLogger({ enableMethodLogging: true }); + class Root extends Component { constructor(props) { super(props); From 8acfbc8d62c83f561911dacea8428b1e6337c652 Mon Sep 17 00:00:00 2001 From: IljaDaderko Date: Wed, 28 Aug 2019 17:01:52 +0300 Subject: [PATCH 2/9] add colors and args --- .../app/lib/internal/registry/nativeModule.js | 11 ++++-- packages/app/lib/utils/logger.js | 34 +++++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/packages/app/lib/internal/registry/nativeModule.js b/packages/app/lib/internal/registry/nativeModule.js index ca0517d8c1..bba5ffa784 100644 --- a/packages/app/lib/internal/registry/nativeModule.js +++ b/packages/app/lib/internal/registry/nativeModule.js @@ -38,10 +38,10 @@ function nativeModuleKey(module) { * @param argToPrepend * @returns {Function} */ -function nativeModuleMethodWrapped(namespace, method, argToPrepend) { +function nativeModuleMethodWrapped(namespace, method, argToPrepend, methodName) { return (...args) => { if (Logger.config.enableMethodLogging) { - Logger.info(`${namespace} - ${method.name} - ${JSON.stringify(args)}`); + Logger.info(`${namespace} -> ${methodName}`, args); } const possiblePromise = method(...[...argToPrepend, ...args]); @@ -75,7 +75,12 @@ function nativeModuleWrapped(namespace, NativeModule, argToPrepend) { for (let i = 0, len = properties.length; i < len; i++) { const property = properties[i]; if (typeof NativeModule[property] === 'function') { - native[property] = nativeModuleMethodWrapped(namespace, NativeModule[property], argToPrepend); + native[property] = nativeModuleMethodWrapped( + namespace, + NativeModule[property], + argToPrepend, + property, + ); } else { native[property] = NativeModule[property]; } diff --git a/packages/app/lib/utils/logger.js b/packages/app/lib/utils/logger.js index 2e053df9b7..e562c6c0ae 100644 --- a/packages/app/lib/utils/logger.js +++ b/packages/app/lib/utils/logger.js @@ -1,10 +1,40 @@ +/* eslint-disable no-console */ +/** + * List of ansi colors + * @url https://github.com/shiena/ansicolor/blob/master/README.md + */ + +import { isArray, isString } from '@react-native-firebase/app/lib/common'; + const config = { enableMethodLogging: false, enableEventLogging: false, }; -function info(text) { - console.log(text); +/** + * Resets terminal to default color + */ +function resetTerminalColor() { + console.log('\x1b[0m'); +} + +/** + * Info level log + * @param {String} text + * @param {Array} params + */ +function info(text, params = null) { + if (!isString(text)) { + throw new Error(`Invalid text passed to logger. Expected string, but got ${typeof text}`); + } + console.log('\x1b[35m', text); + + if (!isArray(params)) { + throw new Error(`Invalid params passed to logger. Expected array, but got ${typeof params}`); + } + console.log('\x1b[94m', JSON.stringify(params, null, 2)); + + resetTerminalColor(); } export default { From 86d42bfd0919ef9a52ce44441d2633c1bbbea20e Mon Sep 17 00:00:00 2001 From: IljaDaderko Date: Wed, 28 Aug 2019 17:02:30 +0300 Subject: [PATCH 3/9] allow object as params --- packages/app/lib/utils/logger.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/app/lib/utils/logger.js b/packages/app/lib/utils/logger.js index e562c6c0ae..89b503444d 100644 --- a/packages/app/lib/utils/logger.js +++ b/packages/app/lib/utils/logger.js @@ -4,7 +4,7 @@ * @url https://github.com/shiena/ansicolor/blob/master/README.md */ -import { isArray, isString } from '@react-native-firebase/app/lib/common'; +import { isArray, isObject, isString } from '@react-native-firebase/app/lib/common'; const config = { enableMethodLogging: false, @@ -29,8 +29,10 @@ function info(text, params = null) { } console.log('\x1b[35m', text); - if (!isArray(params)) { - throw new Error(`Invalid params passed to logger. Expected array, but got ${typeof params}`); + if (!isArray(params) && !isObject(params)) { + throw new Error( + `Invalid params passed to logger. Expected array or object, but got ${typeof params}`, + ); } console.log('\x1b[94m', JSON.stringify(params, null, 2)); From 608a6e494896957803e390674361fd22dd531270 Mon Sep 17 00:00:00 2001 From: IljaDaderko Date: Wed, 4 Sep 2019 16:07:10 +0300 Subject: [PATCH 4/9] add logger types, handle default null param value --- packages/app/lib/index.d.ts | 11 +++++++++++ packages/app/lib/utils/logger.js | 8 +++++--- packages/app/type-test.ts | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/app/lib/index.d.ts b/packages/app/lib/index.d.ts index 31790b2297..eacaff2d8d 100644 --- a/packages/app/lib/index.d.ts +++ b/packages/app/lib/index.d.ts @@ -240,6 +240,7 @@ export namespace ReactNativeFirebase { */ export namespace Utils { import FirebaseModule = ReactNativeFirebase.FirebaseModule; + type LoggerConfig = { enableMethodLogging: boolean; enableEventLogging: boolean }; /** * A collection of native device file paths to aid in the usage of file path based methods. @@ -379,6 +380,16 @@ export namespace Utils { * @android */ isRunningInTestLab: boolean; + + /** + * Enables logging based on configuration that was passed in + */ + enableLogger: (config: LoggerConfig) => void; + + /** + * Returns logger instance + */ + logger: { config: LoggerConfig; info: (text: string, params?: object | T[] | null) => void }; } } diff --git a/packages/app/lib/utils/logger.js b/packages/app/lib/utils/logger.js index 89b503444d..3e89a64967 100644 --- a/packages/app/lib/utils/logger.js +++ b/packages/app/lib/utils/logger.js @@ -4,7 +4,7 @@ * @url https://github.com/shiena/ansicolor/blob/master/README.md */ -import { isArray, isObject, isString } from '@react-native-firebase/app/lib/common'; +import { isArray, isNull, isObject, isString } from '@react-native-firebase/app/lib/common'; const config = { enableMethodLogging: false, @@ -29,12 +29,14 @@ function info(text, params = null) { } console.log('\x1b[35m', text); - if (!isArray(params) && !isObject(params)) { + if (!isArray(params) && !isObject(params) && !isNull(params)) { throw new Error( `Invalid params passed to logger. Expected array or object, but got ${typeof params}`, ); } - console.log('\x1b[94m', JSON.stringify(params, null, 2)); + if (params) { + console.log('\x1b[94m', JSON.stringify(params, null, 2)); + } resetTerminalColor(); } diff --git a/packages/app/type-test.ts b/packages/app/type-test.ts index 577e82afb4..ae74998929 100644 --- a/packages/app/type-test.ts +++ b/packages/app/type-test.ts @@ -32,6 +32,10 @@ console.log(utils.FilePath.CACHES_DIRECTORY); console.log(firebase.utils.FilePath.CACHES_DIRECTORY); console.log(utils.FilePath.CACHES_DIRECTORY); +// checks logger +console.log(firebase.utils().logger); +console.log(firebase.utils().enableLogger); + // checks root exists console.log(firebase.SDK_VERSION); From 2fccc5b6008778b6e9cc1e9fa38d433c9bd1554b Mon Sep 17 00:00:00 2001 From: IljaDaderko Date: Wed, 4 Sep 2019 16:14:31 +0300 Subject: [PATCH 5/9] add logger documentation --- docs/utils/quick-start.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/utils/quick-start.md b/docs/utils/quick-start.md index 2c34a5eaf5..86e9e05779 100644 --- a/docs/utils/quick-start.md +++ b/docs/utils/quick-start.md @@ -53,3 +53,35 @@ import firebase from '@react-native-firebase/app'; // Access the device pictures directory const picturesDir = firebase.utils.FilePath.PICTURES_DIRECTORY; ``` + +### Enable logging + +You can enable logging for native method calls and event streams + +```js +import firebase from '@react-native-firebase/app'; + +firebase.utils().enableLogger({ + enableMethodLogging: true, + enableEventLogging: true, +}); +``` + +### Log out custom information + +You can also use our built in logger to log your custom information + +```js +import firebase from '@react-native-firebase/app'; + +firebase.utils.info('Custom Log'); +``` + +Info logger also takes in second argument that can be object or array, this is usefull +when you need to log some additional data. + +```js +import firebase from '@react-native-firebase/app'; + +firebase.utils.info('Custom Log', { uid: '123' }); +``` From dfc489e3b883aea61c1f97c2edea204bfbcb1b64 Mon Sep 17 00:00:00 2001 From: IljaDaderko Date: Wed, 4 Sep 2019 17:14:04 +0300 Subject: [PATCH 6/9] Add logger tests, documentation and types --- docs/utils/quick-start.md | 9 ++ packages/app/e2e/utils.e2e.js | 84 +++++++++++++++++++ packages/app/lib/index.d.ts | 2 +- .../app/lib/internal/registry/nativeModule.js | 10 ++- packages/app/lib/utils/index.js | 17 +++- 5 files changed, 119 insertions(+), 3 deletions(-) diff --git a/docs/utils/quick-start.md b/docs/utils/quick-start.md index 86e9e05779..8522710a49 100644 --- a/docs/utils/quick-start.md +++ b/docs/utils/quick-start.md @@ -65,6 +65,15 @@ firebase.utils().enableLogger({ enableMethodLogging: true, enableEventLogging: true, }); + +// It also returns back config + +const config = firebase.utils().enableLogger({ + enableMethodLogging: true, + enableEventLogging: true, +}); + +// config = { enableMethodLogging: true, enableEventLogging: ture } ``` ### Log out custom information diff --git a/packages/app/e2e/utils.e2e.js b/packages/app/e2e/utils.e2e.js index f90432a3d2..36b72e9287 100644 --- a/packages/app/e2e/utils.e2e.js +++ b/packages/app/e2e/utils.e2e.js @@ -29,4 +29,88 @@ describe('utils()', () => { should.equal(firebase.utils().isRunningInTestLab, false); }); }); + + describe('logger', () => { + it('throws if config is not an object', () => { + try { + firebase.utils().enableLogger(5); + return Promise.reject(new Error('Did not throw Error.')); + } catch (e) { + e.message.should.containEql('Invalid config passed to enableLogger'); + return Promise.resolve(); + } + }); + + it("throws if config object doesn't contain at least one valid option", () => { + try { + firebase.utils().enableLogger({ invalidOption: true }); + return Promise.reject(new Error('Did not throw Error.')); + } catch (e) { + e.message.should.containEql( + 'enableLogger expects at least one option: enableMethodLogging or enableEventLogging', + ); + return Promise.resolve(); + } + }); + + it('enables logging correctly', () => { + const config = firebase + .utils() + .enableLogger({ enableMethodLogging: true, enableEventLogging: true }); + config.enableMethodLogging.should.eql(true); + config.enableEventLogging.should.eql(true); + }); + + it('throws if non string text was passed to .info', () => { + try { + firebase + .utils() + .logger() + .info(123); + return Promise.reject(new Error('Did not throw Error.')); + } catch (e) { + e.message.should.containEql( + 'Invalid text passed to logger. Expected string, but got number', + ); + return Promise.resolve(); + } + }); + + it('logs correctly', () => { + try { + firebase + .utils() + .logger() + .info('Custom Log'); + } catch (e) { + Promise.reject(new Error(e.message)); + } + }); + + it('throws if incorrect params were passed', () => { + try { + firebase + .utils() + .logger() + .info('Custom Log', 123); + return Promise.reject(new Error('Did not throw Error.')); + } catch (e) { + e.message.should.containEql( + 'Invalid params passed to logger. Expected array or object, but got number', + ); + return Promise.resolve(); + } + }); + + it('logs correctly with params', () => { + try { + firebase + .utils() + .logger() + .info('Custom Log', { uid: 123 }); + } catch (e) { + Promise.reject(new Error(e.message)); + } + }); + }); }); diff --git a/packages/app/lib/index.d.ts b/packages/app/lib/index.d.ts index eacaff2d8d..253d7852b1 100644 --- a/packages/app/lib/index.d.ts +++ b/packages/app/lib/index.d.ts @@ -384,7 +384,7 @@ export namespace Utils { /** * Enables logging based on configuration that was passed in */ - enableLogger: (config: LoggerConfig) => void; + enableLogger: (config: LoggerConfig) => LoggerConfig; /** * Returns logger instance diff --git a/packages/app/lib/internal/registry/nativeModule.js b/packages/app/lib/internal/registry/nativeModule.js index bba5ffa784..844125fa4f 100644 --- a/packages/app/lib/internal/registry/nativeModule.js +++ b/packages/app/lib/internal/registry/nativeModule.js @@ -41,7 +41,7 @@ function nativeModuleKey(module) { function nativeModuleMethodWrapped(namespace, method, argToPrepend, methodName) { return (...args) => { if (Logger.config.enableMethodLogging) { - Logger.info(`${namespace} -> ${methodName}`, args); + Logger.info(`METHOD:${namespace}::${methodName}`, args); } const possiblePromise = method(...[...argToPrepend, ...args]); @@ -160,11 +160,19 @@ function initialiseNativeModule(module) { */ function subscribeToNativeModuleEvent(eventName) { if (!NATIVE_MODULE_EVENT_SUBSCRIPTIONS[eventName]) { + const eventLoggingEnabled = Logger.config.enableEventLogging; + RNFBNativeEventEmitter.addListener(eventName, event => { if (event.appName) { + if (eventLoggingEnabled) { + Logger.info(`EVENT:${eventName}::${appName}`, args); + } // native event has an appName property - auto prefix and internally emit SharedEventEmitter.emit(`${event.appName}-${eventName}`, event); } else { + if (eventLoggingEnabled) { + Logger.info(`EVENT:${eventName}`, args); + } // standard event - no need to prefix SharedEventEmitter.emit(eventName, event); } diff --git a/packages/app/lib/utils/index.js b/packages/app/lib/utils/index.js index cfe61ec5f5..c511bf1eeb 100644 --- a/packages/app/lib/utils/index.js +++ b/packages/app/lib/utils/index.js @@ -15,7 +15,7 @@ * */ -import { isIOS } from '../../lib/common'; +import { hasOwnProperty, isIOS, isObject } from '../../lib/common'; import { createModuleNamespace, FirebaseModule } from '../../lib/internal'; import Logger from './logger'; import UtilsStatics from './UtilsStatics'; @@ -33,7 +33,22 @@ class FirebaseUtilsModule extends FirebaseModule { } enableLogger(config) { + if (!isObject(config)) { + throw new Error('Invalid config passed to enableLogger'); + } + + if ( + !hasOwnProperty(config, 'enableMethodLogging') && + !hasOwnProperty(config, 'enableEventLogging') + ) { + throw new Error( + 'enableLogger expects at least one option: enableMethodLogging or enableEventLogging', + ); + } + Logger.config = { ...Logger.config, ...config }; + + return Logger.config; } logger() { From 6f8213ce7622a6ffd72ee9c04e1dc1f4d6ac1e49 Mon Sep 17 00:00:00 2001 From: IljaDaderko Date: Wed, 4 Sep 2019 17:14:55 +0300 Subject: [PATCH 7/9] disable logging by default --- tests/app.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/app.js b/tests/app.js index 6be40e57fa..7011a15110 100644 --- a/tests/app.js +++ b/tests/app.js @@ -44,8 +44,6 @@ jet.exposeContextProperty('NativeModules', NativeModules); jet.exposeContextProperty('NativeEventEmitter', NativeEventEmitter); jet.exposeContextProperty('module', firebase); -firebase.utils().enableLogger({ enableMethodLogging: true }); - class Root extends Component { constructor(props) { super(props); From 04e9f66140aa978a9cab780e26fcb107424da002 Mon Sep 17 00:00:00 2001 From: IljaDaderko Date: Wed, 4 Sep 2019 17:23:31 +0300 Subject: [PATCH 8/9] formating --- packages/auth/lib/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/auth/lib/index.d.ts b/packages/auth/lib/index.d.ts index 85bb6963ab..4eb52ba9af 100644 --- a/packages/auth/lib/index.d.ts +++ b/packages/auth/lib/index.d.ts @@ -544,8 +544,8 @@ export namespace Auth { */ export interface ActionCodeInfo { /** - * The data associated with the action code. - */ + * The data associated with the action code. + */ data: ActionCodeInfoData; /** * The operation from where the action originated. From 82573c72aa93e1ad6b01418287d423b100f343cd Mon Sep 17 00:00:00 2001 From: IljaDaderko Date: Thu, 5 Sep 2019 10:08:37 +0300 Subject: [PATCH 9/9] address pr comments --- docs/utils/quick-start.md | 6 +++--- packages/app/lib/index.d.ts | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/utils/quick-start.md b/docs/utils/quick-start.md index 8522710a49..4cb0839bf0 100644 --- a/docs/utils/quick-start.md +++ b/docs/utils/quick-start.md @@ -56,7 +56,7 @@ const picturesDir = firebase.utils.FilePath.PICTURES_DIRECTORY; ### Enable logging -You can enable logging for native method calls and event streams +You can enable logging for native method calls and event streams. ```js import firebase from '@react-native-firebase/app'; @@ -73,12 +73,12 @@ const config = firebase.utils().enableLogger({ enableEventLogging: true, }); -// config = { enableMethodLogging: true, enableEventLogging: ture } +// config = { enableMethodLogging: true, enableEventLogging: true } ``` ### Log out custom information -You can also use our built in logger to log your custom information +You can also use our built in logger to log your custom information. ```js import firebase from '@react-native-firebase/app'; diff --git a/packages/app/lib/index.d.ts b/packages/app/lib/index.d.ts index 253d7852b1..4e397a2688 100644 --- a/packages/app/lib/index.d.ts +++ b/packages/app/lib/index.d.ts @@ -240,6 +240,11 @@ export namespace ReactNativeFirebase { */ export namespace Utils { import FirebaseModule = ReactNativeFirebase.FirebaseModule; + + /** + * Logger configuration object you can use to enable internal RNFB logging + * for methods and events. + */ type LoggerConfig = { enableMethodLogging: boolean; enableEventLogging: boolean }; /**