{ "source": "doc/api/console.markdown", "modules": [ { "textRaw": "Console", "name": "console", "stability": 2, "stabilityText": "Stable", "desc": "
The module defines a Console
class and exports a console
object.\n\n
The console
object is a special instance of Console
whose output is\nsent to stdout or stderr.\n\n
For ease of use, console
is defined as a global object and can be used\ndirectly without require
.\n\n
For printing to stdout and stderr. Similar to the console object functions\nprovided by most web browsers, here the output is sent to stdout or stderr.\n\n
\nThe 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 it's a pipe (to avoid blocking for long periods of time).\n\n
\nThat is, in the following example, stdout is non-blocking while stderr\nis blocking:\n\n
\n$ node script.js 2> error.log | tee info.log
\nIn daily use, the blocking/non-blocking dichotomy is not something you\nshould worry about unless you log huge amounts of data.\n\n\n
\n", "methods": [ { "textRaw": "console.log([data][, ...])", "type": "method", "name": "log", "desc": "Prints to stdout with newline. This function can take multiple arguments in a\nprintf()
-like way. Example:\n\n
var count = 5;\nconsole.log('count: %d', count);\n// prints 'count: 5'
\nIf formatting elements are not found in the first string then util.inspect
\nis used on each argument. See [util.format()][] for more information.\n\n
Same as console.log
.\n\n
Same as console.log
but prints to stderr.\n\n
Same as console.error
.\n\n
Uses util.inspect
on obj
and prints resulting string to stdout. This function\nbypasses any custom inspect()
function on obj
. An optional options object\nmay be passed that alters certain aspects of the formatted string:\n\n
showHidden
- if true
then the object's non-enumerable and symbol\nproperties will be shown too. Defaults to false
.
depth
- tells inspect
how many times to recurse while formatting the\nobject. This is useful for inspecting large complicated objects. Defaults to\n2
. To make it recurse indefinitely pass null
.
colors
- if true
, then the output will be styled with ANSI color codes.\nDefaults to false
. Colors are customizable, see below.
Used to calculate the duration of a specific operation. To start a timer, call\nthe console.time()
method, giving it a name as only parameter. To stop the\ntimer, and to get the elapsed time in milliseconds, just call the\nconsole.timeEnd()
method, again passing the\ntimer's name as the parameter.\n\n
Stops a timer that was previously started by calling\nconsole.time()
and print the result to the\nconsole.\n\n
Example:\n\n
\nconsole.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": "Print to stderr 'Trace :'
, followed by the formatted message and stack trace\nto the current position.\n\n
Similar to [assert.ok()][], but the error message is formatted as\nutil.format(message...)
.\n\n
Use require('console').Console
or console.Console
to access this class.\n\n
var Console = require('console').Console;\nvar Console = console.Console;
\nYou can use Console
class to custom simple logger like console
, but with\ndifferent output streams.\n\n
Create 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
var output = fs.createWriteStream('./stdout.log');\nvar errorOutput = fs.createWriteStream('./stderr.log');\n// custom simple logger\nvar logger = new Console(output, errorOutput);\n// use it like console\nvar count = 5;\nlogger.log('count: %d', count);\n// in stdout.log: count 5
\nThe global console
is a special Console
whose output is sent to\nprocess.stdout
and process.stderr
:\n\n
new Console(process.stdout, process.stderr);
\n"
}
]
}
],
"type": "module",
"displayName": "Console"
}
]
}