1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
# Logging
GJS includes a number of built-in functions for logging and aiding debugging, in
addition to those available as a part of the GNOME APIs.
In most cases, the [`console`][console] suite of functions should be preferred
for logging in GJS.
#### Import
The functions in this module are available globally, without import.
[console]: https://gjs-docs.gnome.org/gjs/console.md
### log(message)
> See also: [`console.log()`][console-log]
Type:
* Static
Parameters:
* message (`Any`) — A string or any coercible value
Logs a message with severity equal to
[`GLib.LogLevelFlags.LEVEL_MESSAGE`][gloglevelflagsmessage].
```js
// expected output: JS LOG: Some message
log('Some message');
// expected output: JS LOG: [object Object]
log({key: 'value'});
```
[console-log]: https://gjs-docs.gnome.org/gjs/console.md#console-log
[gloglevelflagsmessage]: https://gjs-docs.gnome.org/glib20/glib.loglevelflags#default-level_message
### logError(error, prefix)
> See also: [`console.trace()`][console-trace]
Type:
* Static
Parameters:
* error (`Error`) — An `Error` or [`GLib.Error`][gerror] object
* prefix (`String`) — Optional prefix for the message
Logs a stack trace for `error`, with an optional prefix, with severity equal to
[`GLib.LogLevelFlags.LEVEL_WARNING`][gloglevelflagswarning].
This function is commonly used in conjunction with `try...catch` blocks to log
errors while still trapping the exception:
```js
try {
throw new Error('Some error occured');
} catch (e) {
logError(e, 'FooError');
}
```
It can also be passed directly to the `catch()` clause of a `Promise` chain:
```js
Promise.reject().catch(logError);
```
[console-trace]: https://gjs-docs.gnome.org/gjs/console.md#console-trace
[gerror]: https://gjs-docs.gnome.org/glib20/glib.error
[gloglevelflagswarning]: https://gjs-docs.gnome.org/glib20/glib.loglevelflags#default-level_warning
### print(...messages)
> Note: this function is not useful for GNOME Shell extensions
Type:
* Static
Parameters:
* messages (`Any`) — Any number of strings or coercible values
Takes any number of strings (or values that can be coerced to strings), joins
them with a space and appends a newline character (`\n`).
The resulting string is printed directly to `stdout` of the current process with
[`g_print()`][gprint].
```js
$ gjs -c "print('foobar', 42, {});"
foobar 42 [object Object]
$
```
[gprint]: https://docs.gtk.org/glib/func.print.html
### printerr(...messages)
> Note: this function is not useful for GNOME Shell extensions
Type:
* Static
Parameters:
* messages (`Any`) — Any number of strings or coercible values
Takes any number of strings (or values that can be coerced to strings), joins
them with a space and appends a newline character (`\n`).
The resulting string is printed directly to `stderr` of the current process with
[`g_printerr()`][gprinterr].
```js
$ gjs -c "printerr('foobar', 42, {});"
foobar 42 [object Object]
$
```
[gprinterr]: https://docs.gtk.org/glib/func.printerr.html
|