How to do `tail -f logfile.txt`-like processing in node.js?

后端 未结 5 2224
遇见更好的自我
遇见更好的自我 2020-12-24 01:19

tail -f logfile.txt outputs the last 10 lines of logfile.txt, and then continues to output appended data as the file grows.

What\'s the recommended way

5条回答
  •  温柔的废话
    2020-12-24 01:55

    The canonical way to do this is with fs.watchFile.

    Alternatively, you could just use the node-tail module, which uses fs.watchFile internally and has already done the work for you. Here is an example of using it straight from the documentation:

    Tail = require('tail').Tail;
    
    tail = new Tail("fileToTail");
    
    tail.on("line", function(data) {
      console.log(data);
    });
    

提交回复
热议问题