Console

introduce

Console is similar to the Console in a browser environment.

The constructor

const fs = require('fs');
const { Console } = require('console');

// Stdout and stderr are log output streams and error output streams respectively.
const logger = new Console({
  stdout: fs.createWriteStream('./stdout.log', { flags: 'a' }),
  stderr: fs.createWriteStream('./stderr.log', { flags: 'a'})});// Write the contents to 'stdout.log' and 'stderr.log' respectively
logger.log(`The ${Date.now()} - log`);
logger.error(`The ${Date.now()} - err`);

// console is a global console instance that does not need to be imported,
// Stdout and stderr are process.stdout and process.stderr respectively
console.log('hello world! ');
Copy the code

methods

log/debug/info

// Output to stdout
console.log('hello world! ');
Copy the code

error/warn

// Output to stderr
console.log('byebye world! ');
Copy the code

trace

// Start with 'Trace: 'and output the contents and stack Trace to stderr
console.trace('follow me! ');
// Output is as follows:
// Trace: follow me!
// at Object.
      
        (xxxxxx\test.js:1:9)
      
// at Module._compile (internal/modules/cjs/loader.js:956:30)
// at Object.Module._extensions.. js (internal/modules/cjs/loader.js:973:10)
// at Module.load (internal/modules/cjs/loader.js:812:32)
// at Function.Module._load (internal/modules/cjs/loader.js:724:14)
// at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
// at internal/main/run_main_module.js:17:11
Copy the code

table

// If the content is' neat ', output it as a table
const heros = [{ name: 'Ashe' }, { name: 'Garen' }, { name: 'Ryze' }];
console.table(heros);
// Output is as follows:
/ / ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
// │ (index) │ name │
/ / ├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤
// │ 0 │ 'Ashe' │ │
// │ 1 │ 'Garen' │
// │ 2 │ 'Ryze' │
/ / └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
Copy the code

Dir and dirxml

// Similar to the dir and dirxml methods in browsers, you can pass in configuration parameters,
ShowHidden: Whether to display the object's non-enumerable properties and Symbol properties
// depth: Object formatting depth, which is infinite when set to null
// colors: colors
const obj = {
  a: { b: { c: { d: { e: { f: { g: { h: 1[Symbol()] :2}}}}}}},};console.log(obj);
/ / output:
// { a: { b: { c: [Object] } } }

console.dir(obj, {
  showHidden: false.depth: null.colors: true});/ / output:
/ / {
// a: {
// b: {
// c: {
// d: {
// e: { f: { g: { h: 1, [Symbol()]: 2 } } }
/ /}
/ /}
/ /}
/ /}
// }
Copy the code

assert

// If the first argument is true, do nothing;
// If the first argument is false, start with 'Assertion failed'
console.assert(true.'nothing'); // There is no output
console.assert(false.'something'); // 'Assertion failed: something'
Copy the code

The count and countReset

// count is used to count;
CountReset is used to reset the count
console.count('a'); // 'a: 1'
console.count('a'); // 'a: 2'
console.count('a'); // 'a: 3'
console.count('b'); // 'b: 1'
console.count('b'); // 'b: 2'
console.count('a'); // 'a: 4'
console.count('b'); // 'b: 3'
console.countReset('a');
console.count('a'); // 'a: 1'
Copy the code

Time, timeLog, and timeEnd

// time is used to start the timer.
// timeLog is used to obtain the timing;
// timeEnd Is used to end the timer
console.time('test');
for (let i = 0; i < 100; i++) {
  if (i % 10= = =0) console.timeLog('test');
}
console.timeEnd('test');
// Output is as follows (total 10+1 lines) :
/ / 'test: 0.617 ms'
/ / 'test: 8.259 ms'
/ / 'test: 8.592 ms'
/ / 'test: 9.516 ms'
/ / 'test: 9.773 ms'
/ / 'test: 10.049 ms'
/ / 'test: 10.297 ms'
/ / 'test: 10.596 ms'
/ / 'test: 11.048 ms'
/ / 'test: 11.347 ms'
/ / 'test: 11.726 ms'
Copy the code

Group/groupCollapsed and groupEnd

// group is used to increment the groupIndentation;
// groupEnd is used to decrement the groupIndentation
const { Console } = require('console');
const indentConsole = new Console({
  stdout: process.stdout,
  groupIndentation: 2}); indentConsole.log('indent-0');
indentConsole.group();
indentConsole.log('indent-2');
indentConsole.group();
indentConsole.log('indent-4');
indentConsole.groupEnd();
indentConsole.log('indent-2');
indentConsole.groupEnd();
indentConsole.log('indent-0');
// Output is as follows:
// 'indent-0'
// 'indent-2'
// 'indent-4'
// 'indent-2'
// 'indent-0'
Copy the code

clear

// This parameter is valid only on terminals to clear the current terminal output
Copy the code