Module console
This module provides functions to write on the standard output stream stdout
and error stream stderr
for error logging and quick debugging.
It’s similar to the console object implemented in most web browsers.
Functions
assert (expression, msg...)
Tests that an expression is true and throws an AssertionError
exception if not. It uses the ECMAScript toBoolean()
convertion.
Example
>> const x = 10;
>> console.assert(x > 0, 'failed!'); // passes
>> console.assert(x < 0, 'failed!'); // fails
AssertionError: failed! at <stdin>:12
>> console.assert(false, 'failed!'); // fails
AssertionError: failed! at <stdin>:13
>> // passes; any Object expression is true
>> console.assert(new Boolean(false), 'failed!');
Parameters
Boolean | expression | the expression to test |
*... | msg... | one or more error messages |
dir (obj)
Prints a list of all properties of an object on stdout
.
Example
>> const obj = { foo: "bar", baz: 12345 };
>> console.dir(obj);
{ foo: 'bar', baz: 12345 }
Parameters
Object | obj | the object whose properties should be output |
error (msg...)
Logs a message with the visual "error" representation, including the file name
and line number of the calling code. Prints on stderr
.
Example
>> console.error('Hello World!');
[error] Hello World! (<stdin>:1)
>> console.error('A: %s, B: %s, C: %s', 'a', 'b', 'c');
[error] A: a, B: b, C: c (<stdin>:3)
>> console.error('Current nanoseconds: %d', java.lang.System.nanoTime());
[error] Current nanoseconds: 9228448561643 (<stdin>:5)
Parameters
*... | msg... | one or more message arguments |
info (msg...)
Logs a message with the visual "info" representation, including the file name
and line number of the calling code. Prints on stdout
.
Example
>> console.info('Hello World!');
[info] Hello World! (<stdin>:1)
>> console.info('A: %s, B: %s, C: %s', 'a', 'b', 'c');
[info] A: a, B: b, C: c (<stdin>:3)
>> console.info('Current nanoseconds: %d', java.lang.System.nanoTime());
[info] Current nanoseconds: 9677228481391 (<stdin>:5)
Parameters
*... | msg... | one or more message arguments |
log (msg...)
Logs a message to the console on stdout
.
The first argument to log may be a string containing printf-like placeholders. Otherwise, multipel arguments will be concatenated separated by spaces.
Example
>> console.log('Hello World!');
Hello World!
>> console.log('A: %s, B: %s, C: %s', 'a', 'b', 'c');
A: a, B: b, C: c
>> console.log('Current nanoseconds: %d', java.lang.System.nanoTime());
Current nanoseconds: 9607196939209
Parameters
*... | msg... | one or more message arguments |
time (name)
Creates a new timer under the given name. Call console.timeEnd(name)
with
the same name to stop the timer and log the time elapsed.
Example
>> console.time('timer-1');
>> // Wait some time ...
>> console.timeEnd('timer-1');
timer-1: 15769ms
Parameters
String | name | the timer name |
timeEnd (name)
Stops a timer created by a call to console.time(name)
and logs the time elapsed.
Example
>> console.time('timer-1');
>> // Wait some time ...
>> console.timeEnd('timer-1');
timer-1: 15769ms
Parameters
String | name | the timer name |
trace (msg...)
Prints a stack trace of JavaScript execution at the point where it is called.
Prints on stdout
.
Parameters
*... | msg... | optional message arguments |
warn (msg...)
Logs a message with the visual "warn" representation, including the file name
and line number of the calling code. Prints on stderr
.
Example
>> console.warn('Hello World!');
[warn] Hello World! (<stdin>:1)
>> console.warn('A: %s, B: %s, C: %s', 'a', 'b', 'c');
[warn] A: a, B: b, C: c (<stdin>:3)
>> console.warn('Current nanoseconds: %d', java.lang.System.nanoTime());
[warn] Current nanoseconds: 9294672097821 (<stdin>:5)
Parameters
*... | msg... | one or more message arguments |