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

后端 未结 5 2209
遇见更好的自我
遇见更好的自我 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:57

    https://github.com/jandre/always-tail seems a great option if you have to worry about log rotating, example from the readme:

    var Tail = require('always-tail');
    var fs = require('fs');
    var filename = "/tmp/testlog";
    
    if (!fs.existsSync(filename)) fs.writeFileSync(filename, "");
    
    var tail = new Tail(filename, '\n');
    
    tail.on('line', function(data) {
      console.log("got line:", data);
    });
    
    
    tail.on('error', function(data) {
      console.log("error:", data);
    });
    
    tail.watch();
    

提交回复
热议问题