Created
March 14, 2019 17:02
-
-
Save Tug/d42dc65dfbf163b62f05458048be746a to your computer and use it in GitHub Desktop.
Override console experiment for mobile-gutenberg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/react-native-gutenberg-bridge/logger.js b/react-native-gutenberg-bridge/logger.js | |
index 8b09dda3..577e191b 100644 | |
--- a/react-native-gutenberg-bridge/logger.js | |
+++ b/react-native-gutenberg-bridge/logger.js | |
@@ -5,27 +5,27 @@ import { NativeModules } from 'react-native'; | |
const { RNReactNativeGutenbergBridge } = NativeModules; | |
-const logger = { | |
- debug: ( message ) => { | |
- console.debug( message ); | |
- return RNReactNativeGutenbergBridge.debug( message ); | |
- }, | |
- info: ( message ) => { | |
- console.info( message ); | |
- return RNReactNativeGutenbergBridge.info( message ); | |
- }, | |
- log: ( message ) => { | |
- console.log( message ); | |
- return RNReactNativeGutenbergBridge.log( message ); | |
- }, | |
- warn: ( message ) => { | |
- console.warn( message ); | |
- return RNReactNativeGutenbergBridge.warn( message ); | |
- }, | |
- error: ( message ) => { | |
- console.error( message ); | |
- return RNReactNativeGutenbergBridge.error( message ); | |
- }, | |
-}; | |
+const supportedLoggingLevels = [ 'debug', 'info', 'log', 'warn', 'error' ]; | |
+ | |
+// Make a local copy of the console ref in case patch() is called | |
+const consoleOriginal = supportedLoggingLevels.reduce( ( result, property ) => { | |
+ result[ property ] = typeof console[ property ] === 'function' ? console[ property ].bind( console ) : console[ property ]; | |
+ return result; | |
+}, {} ); | |
+ | |
+const logger = supportedLoggingLevels.reduce( ( result, property ) => { | |
+ result[ property ] = ( ...args ) => { | |
+ consoleOriginal[ property ]( ...args ); | |
+ let data; | |
+ try { | |
+ data = JSON.stringify( args ); | |
+ } catch ( err ) { | |
+ // Probably a circular structure being given | |
+ data = ''; | |
+ } | |
+ return RNReactNativeGutenbergBridge.debug( data ); | |
+ }; | |
+ return result; | |
+}, {} ); | |
export default logger; | |
diff --git a/src/globals.js b/src/globals.js | |
index 8456ccf0..a9b23718 100644 | |
--- a/src/globals.js | |
+++ b/src/globals.js | |
@@ -7,6 +7,9 @@ import jsdomLevel1Core from 'jsdom-jscore/lib/jsdom/level1/core'; | |
// Import for side-effects: Patches for jsdom-jscore, details commented in file. | |
import './jsdom-patches'; | |
+import logger from '../react-native-gutenberg-bridge/logger'; | |
+ | |
+global.console = logger; | |
global.wp = { | |
element: { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment