Node.js Library to start tailing a file from specific line or line number?

370 views Asked by At

I am searching for library/module that starts tailing from a specific line number so that even if my server dies down or restarts it should start from the last read line. I am little new to node.js

2

There are 2 answers

0
Anakooter On BEST ANSWER

This is how I tackled the problem.
I maintained two log files one of which use to store the lines already read so even if my server dies down reading the main log file I can skip all the logs I have already logged by comparing the time of last logged line with the time of the main log and skip the lines of main log till the time of my own log is greater than or equal to the main log.
Once the mainlog time is greater I start reading the from that point again.

0
Dejan Toteff On

I am unaware of such module, but all you actually need is to use some NPM library for file read access and then you persist the data about the last read line, so it can resume after Node.js restart. Or even better- you fork a child process and this process is reading the data. If it reach an error, the child process terminates with message to the main Node.js process about the last read line. I hope that helps!