Node.js console.log vs console.info

后端 未结 10 1176
青春惊慌失措
青春惊慌失措 2021-01-30 15:51

What is the benefit of using console.log vs console.info? Or any of the other console commands for that matter?

console.info(\"info\")         


        
相关标签:
10条回答
  • 2021-01-30 15:55

    According to the docs it's pretty clear.

    console.info([data], [...])# Same as console.log.

    console.error([data], [...])# Same as console.log but prints to stderr.

    console.warn([data], [...])# Same as console.error.

    This means there is no benefit or downside. info == log, and warn == error. Unless you want to print to stderr, info and or log will work.

    0 讨论(0)
  • 2021-01-30 15:55

    stdin A readable stream for reading input from the user.

    stdout A writable stream, either synchronously or asynchronously.

    stderr A blocking synchronous writable stream intended for error messages.

    The stdout or non-blocking functions are: console.log, console.info, util.puts, util.print and Stderr.

    The blocking functons are: console.warn, console.error, util.debug and process.stdin (a readable stream for getting user input).

    0 讨论(0)
  • 2021-01-30 16:01

    One more detail in addition to the accepted answer: In Chrome and FireFox, console.info log lines are prefixed with a little i icon while console.log lines are not. warn and error lines are prefixed with a little triangle and x, respectively.

    0 讨论(0)
  • 2021-01-30 16:01

    It has been established that log and info are basically the same thing, but I'm not sure that completely answers the question:

    What is the benefit of using console.log vs console.info?

    One benefit, apart from what has already been mentioned, is you can use each for a different purpose. For example, you might use console.log just for quick debugging and spitting things out to the console, while you might use console.info for permanent messages you want to output to the console in your code, such as information on the current app status. Then when you have a situation where a random object is printed in your console and you realize you've left a log statement in there somewhere by accident, you can do a global search for 'console.log' and delete every instance and have confidence you didn't delete anything important that you meant to leave in there.

    0 讨论(0)
  • 2021-01-30 16:03

    I have seen where console.log is for temporarily logging of state information for debugging.

    console.info is a more permanent thing - such as saying what port something is running on, and something you wouldn't cut out after you are done debugging.

    This makes it easy to clean up your code for committing it. You can even have your linter have a rule to prevent console.log from being committed.

    0 讨论(0)
  • 2021-01-30 16:07

    According to the documentation that you linked to, console.error and console.warn outputs to stderr. The others output to stdout.

    If you are doing piping or redirection from node.js the difference is important.

    There is a lot of JavaScript written to run in both the browser and Node.js. Having node implement the full console allows for greater code cross-compatibility.

    In most browsers, not only do these log in different colors, but you can also filter to see specific messages.

    console.info("info");
    console.error("error");
    console.warn("warn");
    console.log("log");
    
    0 讨论(0)
提交回复
热议问题