Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Node .js console


May 10, 2021 Node.js


Table of contents


Console

稳定性: 4 - 冻结
  • {Object}

Node.js s console module provides a simple debugging console.

Node .js the console is to print output characters to stdout (standard output) and stderr (standard error). Similar to the console object functions provided by most browsers, Node is also output to stdout and stderr.

If the output target is a terminal or file, the console function is synchronized (this is to prevent accidental exit resulting in data loss), and the output is asynchronous when the output is piped (prevent blocking time is too long).

In the following example, stdout is non-blocking, while stderr is blocked:

$ node script.js 2> error.log | tee info.log

During normal use, blocking or non-blocking is considered only when large amounts of data are found.

console.log([data][, ...])

Output to stdout and start a new line. Similar printf() stdout can pass in multiple parameters, such as:

var count = 5;
console.log('count: %d', count);
// prints 'count: 5'

If the formatted element is not found in the first character, util.inspect will be applied to each parameter, see util.format().

console.info([data][, ...])

See console.log

console.error([data][, ...])

See console.log between printing to stderr.

console.warn([data][, ...])

See console.error

console.dir(obj[, options])

In obj util.inspect and print the results to stdout, while this function inspect() options may be passed in to the following:

  • showHidden - If true, true non-enumerate properties are displayed, by default false

  • depth - The inspect recursive objects, which is useful for scanning complex objects. T he default 2 If you want strict recursion, pass null

  • colors - If true the output is formatted as an ANSI color code. T he default is false Colors can be customized, as described below.

console.time(label)

Mark a point in time.

console.timeEnd(label)

At the end of the timer, record the output, for example:

console.time('100-elements');
for (var i = 0; i < 100; i++) {
  ;
}
console.timeEnd('100-elements');
// prints 100-elements: 262ms

console.trace(message[, ...])

The stack that outputs the current position is traced to 'Trace :'

console.assert(value[, message][, ...])

Similar to assert.ok(), but in the wrong output format: util.format(message...)