File tree Expand file tree Collapse file tree 2 files changed +17
-12
lines changed
doc/7/essentials/debugging Expand file tree Collapse file tree 2 files changed +17
-12
lines changed Original file line number Diff line number Diff line change @@ -36,8 +36,6 @@ export DEBUG=kuzzle-sdk
3636# Run your program
3737```
3838
39- In the Browser, you need to add the ` debugKuzzleSdk ` search param in the URL.
40-
41- ```
42- http://my-application.by/devices?debugKuzzleSdk
43- ```
39+ In the Browser, you can:
40+ - add the ` debugKuzzleSdk ` search param in the URL. (e.g. ` http://my-application.by/<path>?debugKuzzleSdk ` )
41+ - set the ` window.debugKuzzleSdk ` variable to ` true `
Original file line number Diff line number Diff line change 1+ let NODE_DEBUG ;
2+
13function shouldDebug ( ) {
24 if ( typeof window === 'undefined' ) {
3- const debugString = process . env . DEBUG || '' ;
5+ // Avoid multiple calls to process.env
6+ if ( ! NODE_DEBUG ) {
7+ NODE_DEBUG = ( process . env . DEBUG || '' ) . includes ( 'kuzzle-sdk' ) ;
8+ }
49
5- return debugString . includes ( 'kuzzle-sdk' ) ;
10+ return NODE_DEBUG ;
611 }
712
8- const url = new URL ( window . location ) ;
9-
10- return url . searchParams . get ( 'debugKuzzleSdk' ) !== null ;
13+ return window . debugKuzzleSdk || new URL ( window . location ) . searchParams . get ( 'debugKuzzleSdk' ) !== null ;
1114}
1215
1316/**
1417 * Print debug only if activated
1518 *
16- * In Node.js, you can set the `DEBUG=kuzzle-sdk` env variable
19+ * In Node.js, you can set the `DEBUG=kuzzle-sdk` env variable.
1720 * In a browser, you can add the `?debugKuzzleSdk` in the URL
21+ * or set `window.debugKuzzleSdk` = true
1822 */
1923function debug ( message , obj ) {
2024 if ( ! shouldDebug ( ) ) {
@@ -25,8 +29,11 @@ function debug (message, obj) {
2529 console . log ( message ) ;
2630
2731 if ( obj ) {
32+ // Browser console can print directly objects
33+ const toPrint = typeof window === 'undefined' ? JSON . stringify ( obj ) : obj ;
34+
2835 // eslint-disable-next-line no-console
29- console . log ( JSON . stringify ( obj ) ) ;
36+ console . log ( toPrint ) ;
3037 }
3138}
3239
You can’t perform that action at this time.
0 commit comments