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
36
36
# Run your program
37
37
```
38
38
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
+
1
3
function shouldDebug ( ) {
2
4
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
+ }
4
9
5
- return debugString . includes ( 'kuzzle-sdk' ) ;
10
+ return NODE_DEBUG ;
6
11
}
7
12
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 ;
11
14
}
12
15
13
16
/**
14
17
* Print debug only if activated
15
18
*
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.
17
20
* In a browser, you can add the `?debugKuzzleSdk` in the URL
21
+ * or set `window.debugKuzzleSdk` = true
18
22
*/
19
23
function debug ( message , obj ) {
20
24
if ( ! shouldDebug ( ) ) {
@@ -25,8 +29,11 @@ function debug (message, obj) {
25
29
console . log ( message ) ;
26
30
27
31
if ( obj ) {
32
+ // Browser console can print directly objects
33
+ const toPrint = typeof window === 'undefined' ? JSON . stringify ( obj ) : obj ;
34
+
28
35
// eslint-disable-next-line no-console
29
- console . log ( JSON . stringify ( obj ) ) ;
36
+ console . log ( toPrint ) ;
30
37
}
31
38
}
32
39
You can’t perform that action at this time.
0 commit comments