{ "source": "doc/api/console.markdown", "modules": [ { "textRaw": "Console", "name": "console", "stability": 2, "stabilityText": "Stable", "desc": "

The console module provides a simple debugging console that is similar to the\nJavaScript console mechanism provided by web browsers.\n\n

\n

The module exports two specific components:\n\n

\n\n

Example using the global console:\n\n

\n
console.log('hello world');\n  // Prints: hello world, to stdout\nconsole.log('hello %s', 'world');\n  // Prints: hello world, to stdout\nconsole.error(new Error('Whoops, something bad happened'));\n  // Prints: [Error: Whoops, something bad happened], to stderr\n\nconst name = 'Will Robinson';\nconsole.warn(`Danger ${name}! Danger!`);\n  // Prints: Danger Will Robinson! Danger!, to stderr
\n

Example using the Console class:\n\n

\n
const out = getStreamSomehow();\nconst err = getStreamSomehow();\nconst myConsole = new console.Console(out, err);\n\nmyConsole.log('hello world');\n  // Prints: hello world, to out\nmyConsole.log('hello %s', 'world');\n  // Prints: hello world, to out\nmyConsole.error(new Error('Whoops, something bad happened'));\n  // Prints: [Error: Whoops, something bad happened], to err\n\nconst name = 'Will Robinson';\nmyConsole.warn(`Danger ${name}! Danger!`);\n  // Prints: Danger Will Robinson! Danger!, to err
\n

While the API for the Console class is designed fundamentally around the\nWeb browser console object, the Console is Node.js is not intended to\nduplicate the browsers functionality exactly.\n\n

\n", "modules": [ { "textRaw": "Asynchronous vs Synchronous Consoles", "name": "asynchronous_vs_synchronous_consoles", "desc": "

The console functions are synchronous when the destination is a terminal or\na file (to avoid lost messages in case of premature exit) and asynchronous\nwhen the destination is a pipe (to avoid blocking for long periods of time).\n\n

\n

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

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

Typically, the distinction between blocking/non-blocking is not important\nunless an application is logging significant amounts of data. High volume\nlogging should use a Console instance that writes to a pipe.\n\n

\n", "type": "module", "displayName": "Asynchronous vs Synchronous Consoles" } ], "classes": [ { "textRaw": "Class: Console", "type": "class", "name": "Console", "desc": "

The Console class can be used to create a simple logger with configurable\noutput streams and can be accessed using either require('console').Console\nor console.Console:\n\n

\n
const Console = require('console').Console;\nconst Console = console.Console;
\n", "methods": [ { "textRaw": "console.assert(value[, message][, ...])", "type": "method", "name": "assert", "desc": "

A simple assertion test that verifies whether value is truthy. If it is not,\nan AssertionError is throw. If provided, the error message is formatted\nusing [util.format()][] and used as the error message.\n\n

\n
console.assert(true, 'does nothing');\n  // OK\nconsole.assert(false, 'Whoops %s', 'didn\\'t work');\n  // AssertionError: Whoops didn't work
\n", "signatures": [ { "params": [ { "name": "value" }, { "name": "message", "optional": true }, { "name": "...", "optional": true } ] } ] }, { "textRaw": "console.dir(obj[, options])", "type": "method", "name": "dir", "desc": "

Uses [util.inspect()][] on obj and prints the resulting string to stdout.\nThis function bypasses any custom inspect() function defined on obj. An\noptional options object may be passed that alters certain aspects of the\nformatted string:\n\n

\n\n", "signatures": [ { "params": [ { "name": "obj" }, { "name": "options", "optional": true } ] } ] }, { "textRaw": "console.error([data][, ...])", "type": "method", "name": "error", "desc": "

Prints to stderr with newline. Multiple arguments can be passed, with the first\nused as the primary message and all additional used as substitution\nvalues similar to printf() (the arguments are all passed to\n[util.format()][]).\n\n

\n
const code = 5;\nconsole.error('error #%d', code);\n  // Prints: error #5, to stderr\nconsole.error('error', code);\n  // Prints: error 5, to stderr
\n

If formatting elements (e.g. %d) are not found in the first string then\n[util.inspect()][] is called on each argument and the resulting string\nvalues are concatenated. See [util.format()][] for more information.\n\n

\n", "signatures": [ { "params": [ { "name": "data", "optional": true }, { "name": "...", "optional": true } ] } ] }, { "textRaw": "console.info([data][, ...])", "type": "method", "name": "info", "desc": "

The console.info() function is an alias for [console.log()][].\n\n

\n", "signatures": [ { "params": [ { "name": "data", "optional": true }, { "name": "...", "optional": true } ] } ] }, { "textRaw": "console.log([data][, ...])", "type": "method", "name": "log", "desc": "

Prints to stdout with newline. Multiple arguments can be passed, with the first\nused as the primary message and all additional used as substitution\nvalues similar to printf() (the arguments are all passed to\n[util.format()][]).\n\n

\n
var count = 5;\nconsole.log('count: %d', count);\n  // Prints: count: 5, to stdout\nconsole.log('count: ', count);\n  // Prints: count: 5, to stdout
\n

If formatting elements (e.g. %d) are not found in the first string then\n[util.inspect()][] is called on each argument and the resulting string\nvalues are concatenated. See [util.format()][] for more information.\n\n

\n", "signatures": [ { "params": [ { "name": "data", "optional": true }, { "name": "...", "optional": true } ] } ] }, { "textRaw": "console.time(label)", "type": "method", "name": "time", "desc": "

Used to calculate the duration of a specific operation. To start a timer, call\nthe console.time() method, giving it a unique label as the only parameter. To stop the\ntimer, and to get the elapsed time in milliseconds, just call the\n[console.timeEnd()][] method, again passing the\ntimer's unique label as the parameter.\n\n

\n", "signatures": [ { "params": [ { "name": "label" } ] } ] }, { "textRaw": "console.timeEnd(label)", "type": "method", "name": "timeEnd", "desc": "

Stops a timer that was previously started by calling [console.time()][] and\nprints the result to stdout:\n\n

\n
console.time('100-elements');\nfor (var i = 0; i < 100; i++) {\n  ;\n}\nconsole.timeEnd('100-elements');\n// prints 100-elements: 262ms
\n", "signatures": [ { "params": [ { "name": "label" } ] } ] }, { "textRaw": "console.trace(message[, ...])", "type": "method", "name": "trace", "desc": "

Prints to stderr the string 'Trace :', followed by the [util.format()][]\nformatted message and stack trace to the current position in the code.\n\n

\n
console.trace('Show me');\n  // Prints: (stack trace will vary based on where trace is called)\n  //  Trace: Show me\n  //    at repl:2:9\n  //    at REPLServer.defaultEval (repl.js:248:27)\n  //    at bound (domain.js:287:14)\n  //    at REPLServer.runBound [as eval] (domain.js:300:12)\n  //    at REPLServer.<anonymous> (repl.js:412:12)\n  //    at emitOne (events.js:82:20)\n  //    at REPLServer.emit (events.js:169:7)\n  //    at REPLServer.Interface._onLine (readline.js:210:10)\n  //    at REPLServer.Interface._line (readline.js:549:8)\n  //    at REPLServer.Interface._ttyWrite (readline.js:826:14)
\n", "signatures": [ { "params": [ { "name": "message" }, { "name": "...", "optional": true } ] } ] }, { "textRaw": "console.warn([data][, ...])", "type": "method", "name": "warn", "desc": "

The console.warn() function is an alias for [console.error()][].\n\n

\n", "signatures": [ { "params": [ { "name": "data", "optional": true }, { "name": "...", "optional": true } ] } ] } ], "signatures": [ { "params": [ { "name": "stdout" }, { "name": "stderr", "optional": true } ], "desc": "

Creates a new Console by passing one or two writable stream instances.\nstdout is a writable stream to print log or info output. stderr\nis used for warning or error output. If stderr isn't passed, the warning\nand error output will be sent to the stdout.\n\n

\n
const output = fs.createWriteStream('./stdout.log');\nconst errorOutput = fs.createWriteStream('./stderr.log');\n// custom simple logger\nconst logger = new Console(output, errorOutput);\n// use it like console\nvar count = 5;\nlogger.log('count: %d', count);\n// in stdout.log: count 5
\n

The global console is a special Console whose output is sent to\nprocess.stdout and process.stderr. It is equivalent to calling:\n\n

\n
new Console(process.stdout, process.stderr);
\n" } ] } ], "type": "module", "displayName": "Console" } ] }