@@ -31,6 +31,13 @@ const styles = {
3131
3232export const pad = ( num , maxLength ) => `${ num } ` . padStart ( 0 , maxLength ) ;
3333
34+ function omit ( keys , object ) {
35+ return Object . entries ( object ) . reduce ( ( acc , [ key , value ] ) => {
36+ acc [ key ] = keys . includes ( key ) ? '_ignored_' : value ;
37+ return acc ;
38+ } , { } ) ;
39+ }
40+
3441function formatTime ( time ) {
3542 return `${ pad ( time . getHours ( ) , 2 ) } :${ pad ( time . getMinutes ( ) , 2 ) } :${ pad (
3643 time . getSeconds ( ) ,
@@ -60,10 +67,16 @@ function renderDiff(diff) {
6067 }
6168}
6269
70+ // debug can be `false`, `true` or `diff`. On production, logging is disabled
71+ // when debug is `undefined` and can be enabled by setting it to either `true`
72+ // or `diff`. On develop, it's enabled when `undefined`, and can be disabled
73+ // by setting it to `false`.
74+ const debug = localStorage . getItem ( 'debug' ) ;
75+ const logLevel = debug === 'false' ? false : debug === 'true' ? true : debug ;
6376const isLoggingEnabled =
64- process . env . NODE_ENV === 'development'
65- ? localStorage . getItem ( 'debug' ) !== false
66- : localStorage . getItem ( 'debug' ) === true ;
77+ process . env . NODE_ENV === 'development' ? logLevel !== false : ! ! logLevel ;
78+ const isDiffEnabled = logLevel === 'diff' ;
79+ const diffIgnoreKeys = [ 'markupEditor' , 'queryEditor' , 'sandbox' ] ;
6780
6881export function withLogging ( reducerFn ) {
6982 if ( ! isLoggingEnabled ) {
@@ -79,7 +92,6 @@ export function withLogging(reducerFn) {
7992 const newState = reducerFn ( prevState , action ) ;
8093
8194 const took = timer . now ( ) - started ;
82- const diff = differ ( prevState , newState ) ;
8395
8496 const header = [
8597 [
@@ -94,7 +106,7 @@ export function withLogging(reducerFn) {
94106 ] ;
95107
96108 if ( supportsGroups ) {
97- console . group ( ...header ) ;
109+ console . groupCollapsed ( ...header ) ;
98110 } else {
99111 console . log ( ...header ) ;
100112 }
@@ -103,29 +115,38 @@ export function withLogging(reducerFn) {
103115 console . log ( '%c action %O' , styles . action , action ) ;
104116 console . log ( '%c new state %O' , styles . nextState , newState ) ;
105117
106- if ( ! diff ) {
107- console . log (
108- '%c diff %cno state change!' ,
109- styles . prevState ,
110- styles . error ,
111- ) ;
118+ if ( ! isDiffEnabled ) {
119+ console . log ( '%c diff %c _disabled_' , styles . prevState , styles . note ) ;
112120 } else {
113- if ( supportsGroups ) {
114- console . groupCollapsed ( ' diff' ) ;
115- } else {
116- console . log ( 'diff' ) ;
117- }
121+ const diff = differ (
122+ omit ( diffIgnoreKeys , prevState ) ,
123+ omit ( diffIgnoreKeys , newState ) ,
124+ ) ;
118125
119- diff . forEach ( ( elem ) => {
126+ if ( ! diff ) {
120127 console . log (
121- ` %c ${ styles . diff [ elem . kind ] . text } ` ,
122- styles . diff [ elem . kind ] . style ,
123- ... renderDiff ( elem ) ,
128+ ' %c diff %cno state change!' ,
129+ styles . prevState ,
130+ styles . error ,
124131 ) ;
125- } ) ;
126-
127- if ( supportsGroups ) {
128- console . groupEnd ( ) ;
132+ } else {
133+ if ( supportsGroups ) {
134+ console . groupCollapsed ( ' diff' ) ;
135+ } else {
136+ console . log ( 'diff' ) ;
137+ }
138+
139+ diff . forEach ( ( elem ) => {
140+ console . log (
141+ `%c ${ styles . diff [ elem . kind ] . text } ` ,
142+ styles . diff [ elem . kind ] . style ,
143+ ...renderDiff ( elem ) ,
144+ ) ;
145+ } ) ;
146+
147+ if ( supportsGroups ) {
148+ console . groupEnd ( ) ;
149+ }
129150 }
130151 }
131152
0 commit comments