Node.js console.log - Is it possible to update a line rather than create a new line?

后端 未结 6 2067
Happy的楠姐
Happy的楠姐 2020-12-07 12:16

My node.js application has a lot of console logs, which are important for me to see (it\'s quite a big app so runs for a long time and I need to know that thing

相关标签:
6条回答
  • 2020-12-07 13:01

    To write a partial line.

    process.stdout.write("text");
    process.stdout.write("more");
    process.stdout.write("\n"); // end the line
    

    If the volume of output is the real issue then you'll probably to rethink your logging. You could use a logging system that allows selective runtime logging to narrow your output to what you need.

    // The sections we want to log and the minimum level
    var LOG_LEVEL = 4;
    var LOG_SECTIONS = ["section1","section2","section3"];
    
    function logit(msg, section, level) {
      if (LOG_SECTIONS.indexOf(section) > -1 && LOG_LEVEL >= level) {
        console.log(section + ":" + msg);
      }
    }
    
    logit("message 1", "section1", 4); // will log
    logit("message 2", "section2", 4); // will log
    logit("message 3", "section3", 2); // wont log, below log level
    logit("message 4", "section4", 4); // wont log, not in a log section
    
    0 讨论(0)
  • 2020-12-07 13:03

    if you see stdout exceptions like TypeError: process.stdout.clearLine is not a function in Debug Console window of Visual Studio Code (or Webstorm), run the app as external terminal application instead of internal console. The reason is that Debug Console window is not TTY (process.stdout.isTTY is false). Therefore update your launch configuration in launch.json with "console": "externalTerminal" option.

    0 讨论(0)
  • 2020-12-07 13:04

    Sure, you can do this using a module I helped create: fknsrs/jetty

    Install via

    npm install jetty
    

    Here's a usage example

    // Yeah, Jetty!
    var Jetty = require("jetty");
    
    // Create a new Jetty object. This is a through stream with some additional
    // methods on it. Additionally, connect it to process.stdout
    var jetty = new Jetty(process.stdout);
    
    // Clear the screen
    jetty.clear();
    
    // write something
    jetty.text("hello world");
    jetty.moveTo([0,0]);
    jetty.text("hello panda");
    

    Jetty is not super useful when used on it's own. It is much more effective when you build some abstraction on top of it to make your jetty calls less verbose.

    0 讨论(0)
  • 2020-12-07 13:04

    Just use \r to terminate your line:

    process.stdout.write('text\r');
    

    Here's a simple example (wall clock):

    setInterval(() => process.stdout.write(`clock: ${new Date()}\r`), 1000);
    
    0 讨论(0)
  • 2020-12-07 13:09

    Following @michelek's answer, you can use a function somewhat like this:

    function printProgress(progress){
        process.stdout.clearLine();
        process.stdout.cursorTo(0);
        process.stdout.write(progress + '%');
    }
    
    0 讨论(0)
  • 2020-12-07 13:12

    Try playing with process.stdout methods instead on console:

    process.stdout.write("Hello, World");
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
    process.stdout.write("\n"); // end the line
    
    0 讨论(0)
提交回复
热议问题