Tuesday 2 October 2018

node.js: Exploring console object

console module is used to log the messages to console. Since console is part of global name space, you no need to import it explicitly.

Console module provides different APIS to log the messages at different levels.

HelloWorld.js
console.trace("Trace Message");
console.debug("Debug Message");
console.info("Information Message");
console.log("Log Message");
console.error("Error Message");
console.warn("Warning Message");

Output
Trace: Trace Message
    at Object.<anonymous> (C:\Users\krishna\Documents\nodejs\examples\HelloWorld.js:1:71)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)
Debug Message
Information Message
Log Message
Error Message
Warning Message


Below table summarizes the methods provided by console module.

S.No
Method And Description
1
If the value is evaluated to false, then the message is logged. If the value is evaluated to true, nothing will happen.
2
‘console.clear()’ method clears the messages on terminal. When stdout is not a TTY, this method does nothing.
3
It maintains internal counter for the label. When you call this method, it prints the number of times console.count() has been called with the given label.

If you do not pass any argument to count() method, then it takes the string 'default' as DEFAULT value.
4
This method resets the counter for this label.
5
Logs the debug message. This function is an alias for console.log().
6
Uses util.inspect() on obj and prints the resulting string to stdout. This function bypasses any custom inspect() function defined on obj.
7
It logs the message and increases indentation of subsequent lines by two spaces.
8
It is an alias for console.group(), logs the message and increases indentation of subsequent lines by two spaces.
9
Decreases indentation of subsequent lines by two spaces.
10
It logs the message to stdout with new line. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values.
11
It logs the message to stdout with new line. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values.
12
If the data is parsed in tabular format, then the data is logged in tabular format, else just log the data normally.
13
it starts a timer, and it is used to compute the duration of an operation.
14
Stops a timer that was previously started by calling console.time() and prints the result to stdout.

15
For a timer that was previously started by calling console.time(), it prints the elapsed time and other data arguments to stdout.
16
print the log message followed by stack trace.



Previous                                                 Next                                                 Home

No comments:

Post a Comment