Reputation: 3882
I'm building a script that reads log files, handles what needs to be handled then writes them to a database
Some log files have a lot of input, multiple times a second Some log files have few to no input at all
Reading the first line of a file, then deleting this line to go to the next one, while I handle the first line, other lines could be added..
fs.readdir('logs/', (err, filenames) => {
filenames.forEach((filename) => {
fs.readFile('logs/'+filename, 'utf-8', (err, content) => {
//processing all new lines (can take multiple ms)
//deleting file
fs.unlink('logs/'+filename)
});
});
});
Is there not a (native or not) method to 'take' first line(s), or take all lines, from a file at once?
Something similar to what the Array.shift() method does to arrays..
Upvotes: 0
Views: 53
Reputation: 2056
Why you are reading the file at once. Instead you can use the node.js streams
.
https://nodejs.org/api/fs.html#fs_class_fs_readstream
This will read the files and output to console
var fs = require('fs');
var readStream = fs.createReadStream('myfile.txt');
readStream.pipe(process.stdout);
You can also go for the npm package node-tail
to read the content of a files while new content written to it.
https://github.com/lucagrulla/node-tail
Upvotes: 1
Reputation: 16157
If your log files has been writen as rotate logs. Example: Each hours has each log file, 9AM.log, 10AM.log....When you process the log files, you can skip current
file and process another files. ex: now is 10:30 AM o’clock, skip file 10AM.log, solve another files.
Upvotes: 1