question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Example use of generator fails with "write after end" error.

See original GitHub issue

The example use of a generator function in the documentation fails with “write after end” error:

Simple program to reproduce this is:

var rfs    = require('rotating-file-stream');

function pad(num) {
    return (num > 9 ? "" : "0") + num;
}

function generator(time, index) {
    if(! time)
        return "file.log";

    var month  = time.getFullYear() + "" + pad(time.getMonth() + 1);
    var day    = pad(time.getDate());
    var hour   = pad(time.getHours());
    var minute = pad(time.getMinutes());

    return "/storage/" + month + "/" + month +
        day + "-" + hour + minute + "-" + index + "-file.log";
}

var stream = rfs(generator, {
    size:     '1M',
    interval: '30m'
});

setInterval(function () {
    stream.write('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
}, 10);

Using node 8.4.0 on Bash on Windows 10.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:23 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
icciccicommented, Aug 17, 2018

Hi @normancarcamo ,

your generator function takes not care neither of parameter index nor of parameter time (apart from first check).

Please pay attention to the note of this paragraph: https://www.npmjs.com/package/rotating-file-stream#function-filenametime-index returned rotated file name must be function of both parameters time and index.

Hope this helps, iCC

0reactions
normancarcamocommented, Aug 17, 2018

@iccicci Thanks bro, I understand and I just did a test of what you have said and its true, but I think that using the following format “hh:mm:ss.log.(index|counter)” is enough for me (well in my case its ok), by the way my issue is gone now, it is working as I’d expect using “pipe” instead of the style “write(chunk)”, here is a copy of my rotator.js config file:

const rfs = require('rotating-file-stream');

let counter = -1;

function generator(time, index) {
  if (!time) return "logs.log";

  const dateiso = new Date().toISOString();
  const datestr = dateiso.substr(0, 10);
  const timestr = dateiso.substr(11, 8);
  counter++;
  console.log('Counter:', counter, 'Index:', index, 'Time:', time); // testing purposes...

  return `${datestr}/${timestr}.log.${counter}`;
}

const stream = rfs(generator, {
  path: `${process.cwd()}/logs`,
  size: '500K',
  interval: '1d',
  maxFiles: 5,
  maxSize: '5M',
  initialRotation: true
});

process.stdin.pipe(stream);

Terminal output:

Counter: 0 Index: 1 Time: 2018-08-17T06:00:00.000Z
Counter: 1 Index: 1 Time: 2018-08-17T06:00:00.000Z
Counter: 2 Index: 1 Time: 2018-08-17T06:00:00.000Z
Counter: 3 Index: 1 Time: 2018-08-17T06:00:00.000Z
Counter: 4 Index: 1 Time: 2018-08-17T06:00:00.000Z
Counter: 5 Index: 1 Time: 2018-08-17T06:00:00.000Z
Counter: 6 Index: 1 Time: 2018-08-17T06:00:00.000Z
Counter: 7 Index: 1 Time: 2018-08-17T06:00:00.000Z
Counter: 8 Index: 1 Time: 2018-08-17T06:00:00.000Z
Counter: 9 Index: 1 Time: 2018-08-17T06:00:00.000Z
Counter: 10 Index: 1 Time: 2018-08-17T06:00:00.000Z

And this is my logs directory: screen shot 2018-08-17 at 2 32 47 am

Oh and to finish I think that using the option “initialRotation” (in my case again) works well instead of ignoring it whenever the option “rotationTime” is set to true.

😃 thanks its a great utility, I was doing this directly using logrotate utility in debian and the problem that I had there was that whenever a file is rotate and new logs arrive to be written they turn out be corrupted due to the “fd” is attached to the initial process that runs it, the solution that I was doing was stop the pid and the reenable it but it looks a bit meesy.

I was looking for packages on npm to see if there was a solution for node and I saw this 😃 and it’s doing it very well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Write after end error when launch server in nodejs
I am beginner at NodeJS and I'm doing a "NodeJS and Express.js full course" at freecodecamp yt and I copied author code which...
Read more >
[core-https] "write after end" test failure on Node 14 #11919
Two tests in core-https are failing on Node 14: 1) NodeHttpsClient should report upload and download progress: RestError: write after end at ...
Read more >
22. Generators - Exploring JS
As a first example, consider the following generator function whose name is genFunc : ... function * genFunc () { throw new Error...
Read more >
Top 10 Most Common Node.js Developer Mistakes - Toptal
Mistake #4: Expecting Callbacks to Run Synchronously​​ Even then, these are often limited to conditional statements, loop statements, and function invocations. ...
Read more >
Understanding Generators in ES6 JavaScript with Examples
Inside the function, we have an infinite while loop. In that loop, we yield the num . When the generator yields, it is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found