0.1.0 • Published 7 years ago
@smpx/tail v0.1.0
@smpx/tail
Watch and tail a file in nodejs tail -f -n 🎉 No Dependencies
Install
npm install @smpx/tailOr with yarn:
yarn add @smpx/tailUse
const tail = require('@smpx/tail');
const stream = tail('/var/log/syslog', {
numLines: 20,
watch: true,
});
stream.on('line', (line) => {
console.log(line);
});
stream.on('error', (err) => {
console.error(err);
});If you want to stop watching:
stream.close();API
tail(filename, options)
filename: Path of the file to tailoptions:bufferSize: Use this bufferSize when reading from the file (default2048)encoding: Encoding of the file (defaultutf8)numLines: Number of lines to read initially (default10) (similar totail -n)watch: Whether to watch the file for changes (defaultfalse) (similar totail -f)- Setting this to true will keep the process alive until you call
close
- Setting this to true will keep the process alive until you call
filter: An optional function to emit only those lines which pass the criteriaconst stream = tail('/var/log/syslog', { numLines: 20, watch: true, filter: (line) => { if (!line) return false; return JSON.parse(line).level === 'error'; } });
Returns:
An eventemitter, with two events, line and error and a function close.
on('line', (line) => {}): emitted whenever we read a new line from the fileon('error', (err) => {}): emitted whenever there's an errorclose: a method to close and unwatch the file
LICENSE
MIT
0.1.0
7 years ago