Server IP : 127.0.0.2 / Your IP : 18.216.82.12 Web Server : Apache/2.4.18 (Ubuntu) System : User : www-data ( ) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : disk_free_space,disk_total_space,diskfreespace,dl,exec,fpaththru,getmyuid,getmypid,highlight_file,ignore_user_abord,leak,listen,link,opcache_get_configuration,opcache_get_status,passthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,php_uname,phpinfo,posix_ctermid,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix,_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_times,posix_ttyname,posix_uname,pclose,popen,proc_open,proc_close,proc_get_status,proc_nice,proc_terminate,shell_exec,source,show_source,system,virtual MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/share/doc/nodejs/api/ |
Upload File : |
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Console Node.js v4.2.6 Manual & Documentation</title> <link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/sh.css"> <link rel="canonical" href="console.html"> </head> <body class="alt apidoc" id="api-section-console"> <div id="content" class="clearfix"> <div id="column2" class="interior"> <div id="intro" class="interior"> <a href="/" title="Go back to the home page"> Node.js (1) </a> </div> <ul> <li><a class="nav-documentation" href="documentation.html">About these Docs</a></li> <li><a class="nav-synopsis" href="synopsis.html">Synopsis</a></li> <li><a class="nav-assert" href="assert.html">Assertion Testing</a></li> <li><a class="nav-buffer" href="buffer.html">Buffer</a></li> <li><a class="nav-addons" href="addons.html">C/C++ Addons</a></li> <li><a class="nav-child_process" href="child_process.html">Child Processes</a></li> <li><a class="nav-cluster" href="cluster.html">Cluster</a></li> <li><a class="nav-console active" href="console.html">Console</a></li> <li><a class="nav-crypto" href="crypto.html">Crypto</a></li> <li><a class="nav-debugger" href="debugger.html">Debugger</a></li> <li><a class="nav-dns" href="dns.html">DNS</a></li> <li><a class="nav-domain" href="domain.html">Domain</a></li> <li><a class="nav-errors" href="errors.html">Errors</a></li> <li><a class="nav-events" href="events.html">Events</a></li> <li><a class="nav-fs" href="fs.html">File System</a></li> <li><a class="nav-globals" href="globals.html">Globals</a></li> <li><a class="nav-http" href="http.html">HTTP</a></li> <li><a class="nav-https" href="https.html">HTTPS</a></li> <li><a class="nav-modules" href="modules.html">Modules</a></li> <li><a class="nav-net" href="net.html">Net</a></li> <li><a class="nav-os" href="os.html">OS</a></li> <li><a class="nav-path" href="path.html">Path</a></li> <li><a class="nav-process" href="process.html">Process</a></li> <li><a class="nav-punycode" href="punycode.html">Punycode</a></li> <li><a class="nav-querystring" href="querystring.html">Query Strings</a></li> <li><a class="nav-readline" href="readline.html">Readline</a></li> <li><a class="nav-repl" href="repl.html">REPL</a></li> <li><a class="nav-stream" href="stream.html">Stream</a></li> <li><a class="nav-string_decoder" href="string_decoder.html">String Decoder</a></li> <li><a class="nav-timers" href="timers.html">Timers</a></li> <li><a class="nav-tls" href="tls.html">TLS/SSL</a></li> <li><a class="nav-tty" href="tty.html">TTY</a></li> <li><a class="nav-dgram" href="dgram.html">UDP/Datagram</a></li> <li><a class="nav-url" href="url.html">URL</a></li> <li><a class="nav-util" href="util.html">Utilities</a></li> <li><a class="nav-v8" href="v8.html">V8</a></li> <li><a class="nav-vm" href="vm.html">VM</a></li> <li><a class="nav-zlib" href="zlib.html">ZLIB</a></li> </ul> </div> <div id="column1" data-id="console" class="interior"> <header> <h1>Node.js v4.2.6 Documentation</h1> <div id="gtoc"> <p> <a href="index.html" name="toc">Index</a> | <a href="all.html">View on single page</a> | <a href="console.json">View as JSON</a> </p> </div> <hr> </header> <div id="toc"> <h2>Table of Contents</h2> <ul> <li><a href="#console_console">Console</a><ul> <li><a href="#console_asynchronous_vs_synchronous_consoles">Asynchronous vs Synchronous Consoles</a></li> <li><a href="#console_class_console">Class: Console</a><ul> <li><a href="#console_new_console_stdout_stderr">new Console(stdout[, stderr])</a></li> <li><a href="#console_console_assert_value_message">console.assert(value[, message][, ...])</a></li> <li><a href="#console_console_dir_obj_options">console.dir(obj[, options])</a></li> <li><a href="#console_console_error_data">console.error([data][, ...])</a></li> <li><a href="#console_console_info_data">console.info([data][, ...])</a></li> <li><a href="#console_console_log_data">console.log([data][, ...])</a></li> <li><a href="#console_console_time_label">console.time(label)</a></li> <li><a href="#console_console_timeend_label">console.timeEnd(label)</a></li> <li><a href="#console_console_trace_message">console.trace(message[, ...])</a></li> <li><a href="#console_console_warn_data">console.warn([data][, ...])</a></li> </ul> </li> </ul> </li> </ul> </div> <div id="apicontent"> <h1>Console<span><a class="mark" href="#console_console" id="console_console">#</a></span></h1> <pre class="api_stability_2">Stability: 2 - Stable</pre><p>The <code>console</code> module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. </p> <p>The module exports two specific components: </p> <ul> <li>A <code>Console</code> class with methods such as <code>console.log()</code>, <code>console.error()</code> and <code>console.warn()</code> that can be used to write to any Node.js stream.</li> <li>A global <code>console</code> instance configured to write to <code>stdout</code> and <code>stderr</code>. Because this object is global, it can be used without calling <code>require('console')</code>.</li> </ul> <p>Example using the global <code>console</code>: </p> <pre><code>console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to stderr const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr</code></pre> <p>Example using the <code>Console</code> class: </p> <pre><code>const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err</code></pre> <p>While the API for the <code>Console</code> class is designed fundamentally around the Web browser <code>console</code> object, the <code>Console</code> is Node.js is <em>not</em> intended to duplicate the browsers functionality exactly. </p> <h2>Asynchronous vs Synchronous Consoles<span><a class="mark" href="#console_asynchronous_vs_synchronous_consoles" id="console_asynchronous_vs_synchronous_consoles">#</a></span></h2> <p>The console functions are synchronous when the destination is a terminal or a file (to avoid lost messages in case of premature exit) and asynchronous when the destination is a pipe (to avoid blocking for long periods of time). </p> <p>In the following example, stdout is non-blocking while stderr is blocking: </p> <pre><code>$ node script.js 2> error.log | tee info.log</code></pre> <p>Typically, the distinction between blocking/non-blocking is not important unless an application is logging significant amounts of data. High volume logging <em>should</em> use a <code>Console</code> instance that writes to a pipe. </p> <h2>Class: Console<span><a class="mark" href="#console_class_console" id="console_class_console">#</a></span></h2> <!--type=class--> <p>The <code>Console</code> class can be used to create a simple logger with configurable output streams and can be accessed using either <code>require('console').Console</code> or <code>console.Console</code>: </p> <pre><code>const Console = require('console').Console; const Console = console.Console;</code></pre> <h3>new Console(stdout[, stderr])<span><a class="mark" href="#console_new_console_stdout_stderr" id="console_new_console_stdout_stderr">#</a></span></h3> <p>Creates a new <code>Console</code> by passing one or two writable stream instances. <code>stdout</code> is a writable stream to print log or info output. <code>stderr</code> is used for warning or error output. If <code>stderr</code> isn't passed, the warning and error output will be sent to the <code>stdout</code>. </p> <pre><code>const output = fs.createWriteStream('./stdout.log'); const errorOutput = fs.createWriteStream('./stderr.log'); // custom simple logger const logger = new Console(output, errorOutput); // use it like console var count = 5; logger.log('count: %d', count); // in stdout.log: count 5</code></pre> <p>The global <code>console</code> is a special <code>Console</code> whose output is sent to <code>process.stdout</code> and <code>process.stderr</code>. It is equivalent to calling: </p> <pre><code>new Console(process.stdout, process.stderr);</code></pre> <h3>console.assert(value[, message][, ...])<span><a class="mark" href="#console_console_assert_value_message" id="console_console_assert_value_message">#</a></span></h3> <p>A simple assertion test that verifies whether <code>value</code> is truthy. If it is not, an <code>AssertionError</code> is throw. If provided, the error <code>message</code> is formatted using <a href="util.html#util_util_format_format"><code>util.format()</code></a> and used as the error message. </p> <pre><code>console.assert(true, 'does nothing'); // OK console.assert(false, 'Whoops %s', 'didn\'t work'); // AssertionError: Whoops didn't work</code></pre> <h3>console.dir(obj[, options])<span><a class="mark" href="#console_console_dir_obj_options" id="console_console_dir_obj_options">#</a></span></h3> <p>Uses <a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> on <code>obj</code> and prints the resulting string to stdout. This function bypasses any custom <code>inspect()</code> function defined on <code>obj</code>. An optional <code>options</code> object may be passed that alters certain aspects of the formatted string: </p> <ul> <li><p><code>showHidden</code> - if <code>true</code> then the object's non-enumerable and symbol properties will be shown too. Defaults to <code>false</code>.</p> </li> <li><p><code>depth</code> - tells <code>inspect</code> how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to <code>2</code>. To make it recurse indefinitely, pass <code>null</code>.</p> </li> <li><p><code>colors</code> - if <code>true</code>, then the output will be styled with ANSI color codes. Defaults to <code>false</code>. Colors are customizable; see [customizing <code>util.inspect()</code> colors][].</p> </li> </ul> <h3>console.error([data][, ...])<span><a class="mark" href="#console_console_error_data" id="console_console_error_data">#</a></span></h3> <p>Prints to stderr with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to <code>printf()</code> (the arguments are all passed to <a href="util.html#util_util_format_format"><code>util.format()</code></a>). </p> <pre><code>const code = 5; console.error('error #%d', code); // Prints: error #5, to stderr console.error('error', code); // Prints: error 5, to stderr</code></pre> <p>If formatting elements (e.g. <code>%d</code>) are not found in the first string then <a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> is called on each argument and the resulting string values are concatenated. See <a href="util.html#util_util_format_format"><code>util.format()</code></a> for more information. </p> <h3>console.info([data][, ...])<span><a class="mark" href="#console_console_info_data" id="console_console_info_data">#</a></span></h3> <p>The <code>console.info()</code> function is an alias for <a href="#console_console_log_data"><code>console.log()</code></a>. </p> <h3>console.log([data][, ...])<span><a class="mark" href="#console_console_log_data" id="console_console_log_data">#</a></span></h3> <p>Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to <code>printf()</code> (the arguments are all passed to <a href="util.html#util_util_format_format"><code>util.format()</code></a>). </p> <pre><code>var count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count: ', count); // Prints: count: 5, to stdout</code></pre> <p>If formatting elements (e.g. <code>%d</code>) are not found in the first string then <a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> is called on each argument and the resulting string values are concatenated. See <a href="util.html#util_util_format_format"><code>util.format()</code></a> for more information. </p> <h3>console.time(label)<span><a class="mark" href="#console_console_time_label" id="console_console_time_label">#</a></span></h3> <p>Used to calculate the duration of a specific operation. To start a timer, call the <code>console.time()</code> method, giving it a unique <code>label</code> as the only parameter. To stop the timer, and to get the elapsed time in milliseconds, just call the <a href="#console_console_timeend_label"><code>console.timeEnd()</code></a> method, again passing the timer's unique <code>label</code> as the parameter. </p> <h3>console.timeEnd(label)<span><a class="mark" href="#console_console_timeend_label" id="console_console_timeend_label">#</a></span></h3> <p>Stops a timer that was previously started by calling <a href="#console_console_time_label"><code>console.time()</code></a> and prints the result to stdout: </p> <pre><code>console.time('100-elements'); for (var i = 0; i < 100; i++) { ; } console.timeEnd('100-elements'); // prints 100-elements: 262ms</code></pre> <h3>console.trace(message[, ...])<span><a class="mark" href="#console_console_trace_message" id="console_console_trace_message">#</a></span></h3> <p>Prints to stderr the string <code>'Trace :'</code>, followed by the <a href="util.html#util_util_format_format"><code>util.format()</code></a> formatted message and stack trace to the current position in the code. </p> <pre><code>console.trace('Show me'); // Prints: (stack trace will vary based on where trace is called) // Trace: Show me // at repl:2:9 // at REPLServer.defaultEval (repl.js:248:27) // at bound (domain.js:287:14) // at REPLServer.runBound [as eval] (domain.js:300:12) // at REPLServer.<anonymous> (repl.js:412:12) // at emitOne (events.js:82:20) // at REPLServer.emit (events.js:169:7) // at REPLServer.Interface._onLine (readline.js:210:10) // at REPLServer.Interface._line (readline.js:549:8) // at REPLServer.Interface._ttyWrite (readline.js:826:14)</code></pre> <h3>console.warn([data][, ...])<span><a class="mark" href="#console_console_warn_data" id="console_console_warn_data">#</a></span></h3> <p>The <code>console.warn()</code> function is an alias for <a href="#console_console_error_data"><code>console.error()</code></a>. </p> </div> </div> </div> <script src="assets/sh_main.js"></script> <script src="assets/sh_javascript.min.js"></script> <script>highlight(undefined, undefined, 'pre');</script> </body> </html>