Coder Social home page Coder Social logo

Comments (9)

schu34 avatar schu34 commented on May 26, 2024 3

Any chance of this being added? It's exactly the functionality I was looking for.

from node-tail.

batjko avatar batjko commented on May 26, 2024 2

I am experimenting with this package and was surprised to find that the "line" event fires for each line as expected, but it gets executed again and again for hundreds of pre-existing lines, even though I added only a single new line at the end of the file.

How can we just catch that last new line (or however many new lines have actually been added)?

from node-tail.

nicolas-goudry avatar nicolas-goudry commented on May 26, 2024 1

For what it’s worth, this package does this out of the box 😜

from node-tail.

hamen avatar hamen commented on May 26, 2024

👍

from node-tail.

lucagrulla avatar lucagrulla commented on May 26, 2024

There's no way at the moment to tail only a certain number of lines. It's either from the beginning of the file or from the latest line. I can see the need of mirroring the tail -n behaviour; the problem is that I can't see a neat way to do this with the current params list; this means I'll have to do some refactoring in the constructor signature(adding yet another params is a no, the signature is getting too long).

from node-tail.

dated avatar dated commented on May 26, 2024

Is this feature planned @lucagrulla?

from node-tail.

lucagrulla avatar lucagrulla commented on May 26, 2024

Unfortunately no.

There's no clean way to idenitfy N lines from file's tail, you can only go via trial and error (read a large of past data, count all the lines, repeat if you missed the mark).

That makes it very inefficient and error prone.
Unless something new appears in the underlying Nodejs core API I'm not looking at doing this any time soon.

from node-tail.

wearhere avatar wearhere commented on May 26, 2024

Here's an implementation that appears to seek character-by-character backward from the end of the file, tracking newlines, until it locates enough lines https://github.com/alexbbt/read-last-lines/blob/master/src/index.js. So, inefficient, yes, error prone, unsure. In any case this doesn't do tailing, just retrieves the last N lines, unfortunately.

What I would be most curious about would be how to merge such an implementation with tailing, making sure that while you were progressively seeking from the end of the file, you also were keeping track of new data appended to the file. I personally would take the hit on efficiency so long as the implementation was correct (in that way), because I suspect that the implementation would overall be faster and more memory-efficient for long logfiles than what I am currently doing, which is

// The 'tail' module lacks a `-n`-like option to get the last `N` lines. So what we
// do is load the entire file, but wait to render only the last `N` lines, then
// start streaming.
const N = 100;
const logs = [];
let initialDataFlushed = false;
const tail = new Tail(logfile, { fromBeginning: true });
tail
  .on('line', (line) => {
    if (initialDataFlushed) {
      console.log(line);
    } else {
      logs.push(line);
      if (logs.length > N) logs.shift();
    }
  })
  .on('historicalDataEnd', () => {
    logs.forEach((line) => console.log(line));
    logs = [];
    initialDataFlushed = true;
  })
  .on('error', (err) => console.error(err));

from node-tail.

lucagrulla avatar lucagrulla commented on May 26, 2024

Available since v.2.2.0.

from node-tail.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.