22
33 Stability: 5 - Locked
44
5- These functions are in the module ` 'util' ` . Use ` require('util') ` to access
6- them.
5+ These functions are in the module ` 'util' ` . Use ` require('util') ` to
6+ access them.
77
8+ The ` util ` module is primarily designed to support the needs of Node's
9+ internal APIs. Many of these utilities are useful for your own
10+ programs. If you find that these functions are lacking for your
11+ purposes, however, you are encouraged to write your own utilities. We
12+ are not interested in any future additions to the ` util ` module that
13+ are unnecessary for Node's internal functionality.
14+
15+ ## util.debuglog(section)
16+
17+ * ` section ` {String} The section of the program to be debugged
18+ * Returns: {Function} The logging function
19+
20+ This is used to create a function which conditionally writes to stderr
21+ based on the existence of a ` NODE_DEBUG ` environment variable. If the
22+ ` section ` name appears in that environment variable, then the returned
23+ function will be similar to ` console.error() ` . If not, then the
24+ returned function is a no-op.
25+
26+ For example:
27+
28+ ``` javascript
29+ var debuglog = util .debuglog (' foo' );
30+
31+ var bar = 123 ;
32+ debuglog (' hello from foo [%d]' , bar);
33+ ```
34+
35+ If this program is run with ` NODE_DEBUG=foo ` in the environment, then
36+ it will output something like:
37+
38+ FOO 3245: hello from foo [123]
39+
40+ where ` 3245 ` is the process id. If it is not run with that
41+ environment variable set, then it will not print anything.
42+
43+ You may separate multiple ` NODE_DEBUG ` environment variables with a
44+ comma. For example, ` NODE_DEBUG=fs,net,tls ` .
845
946## util.format(format, [ ...] )
1047
@@ -37,35 +74,12 @@ Each argument is converted to a string with `util.inspect()`.
3774 util.format(1, 2, 3); // '1 2 3'
3875
3976
40- ## util.debug(string)
41-
42- A synchronous output function. Will block the process and
43- output ` string ` immediately to ` stderr ` .
44-
45- require('util').debug('message on stderr');
46-
47- ## util.error([ ...] )
48-
49- Same as ` util.debug() ` except this will output all arguments immediately to
50- ` stderr ` .
51-
52- ## util.puts([ ...] )
53-
54- A synchronous output function. Will block the process and output all arguments
55- to ` stdout ` with newlines after each argument.
56-
57- ## util.print([ ...] )
58-
59- A synchronous output function. Will block the process, cast each argument to a
60- string then output to ` stdout ` . Does not place newlines after each argument.
61-
6277## util.log(string)
6378
6479Output with timestamp on ` stdout ` .
6580
6681 require('util').log('Timestamped message.');
6782
68-
6983## util.inspect(object, [ options] )
7084
7185Return a string representation of ` object ` , which is useful for debugging.
@@ -94,6 +108,8 @@ Example of inspecting all properties of the `util` object:
94108
95109### Customizing ` util.inspect ` colors
96110
111+ <!-- type=misc -->
112+
97113Color output (if enabled) of ` util.inspect ` is customizable globally
98114via ` util.inspect.styles ` and ` util.inspect.colors ` objects.
99115
@@ -116,6 +132,8 @@ There are also `bold`, `italic`, `underline` and `inverse` codes.
116132
117133### Custom ` inspect() ` function on Objects
118134
135+ <!-- type=misc -->
136+
119137Objects also may define their own ` inspect(depth) ` function which ` util.inspect() `
120138will invoke and use the result of when inspecting the object:
121139
@@ -198,17 +216,6 @@ Returns `true` if the given "object" is an `Error`. `false` otherwise.
198216 // false
199217
200218
201- ## util.pump(readableStream, writableStream, [ callback] )
202-
203- Stability: 0 - Deprecated: Use readableStream.pipe(writableStream)
204-
205- Read the data from ` readableStream ` and send it to the ` writableStream ` .
206- When ` writableStream.write(data) ` returns ` false ` ` readableStream ` will be
207- paused until the ` drain ` event occurs on the ` writableStream ` . ` callback ` gets
208- an error as its only argument and is called when ` writableStream ` is closed or
209- when an error occurs.
210-
211-
212219## util.inherits(constructor, superConstructor)
213220
214221Inherit the prototype methods from one
@@ -241,3 +248,35 @@ through the `constructor.super_` property.
241248 console.log('Received data: "' + data + '"');
242249 })
243250 stream.write("It works!"); // Received data: "It works!"
251+
252+
253+ ## util.debug(string)
254+
255+ Stability: 0 - Deprecated: use console.error() instead.
256+
257+ Deprecated predecessor of ` console.error ` .
258+
259+ ## util.error([ ...] )
260+
261+ Stability: 0 - Deprecated: Use console.error() instead.
262+
263+ Deprecated predecessor of ` console.error ` .
264+
265+ ## util.puts([ ...] )
266+
267+ Stability: 0 - Deprecated: Use console.log() instead.
268+
269+ Deprecated predecessor of ` console.log ` .
270+
271+ ## util.print([ ...] )
272+
273+ Stability: 0 - Deprecated: Use `console.log` instead.
274+
275+ Deprecated predecessor of ` console.log ` .
276+
277+
278+ ## util.pump(readableStream, writableStream, [ callback] )
279+
280+ Stability: 0 - Deprecated: Use readableStream.pipe(writableStream)
281+
282+ Deprecated predecessor of ` stream.pipe() ` .
0 commit comments