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.

File logging reconfiguration issue (Error: write after end)

See original GitHub issue

Please tell us about your environment:

  • winston version?
    • winston@2
    • [ X] winston@3
  • _node -v outputs:_v10.15.0
  • _Operating System?_Windows 10
  • _Language? JavaScript (pure Node)

What is the problem?

When logger is reconfigured, the open streams are destroyed and winston tries to write to an already destroyed stream.

I created a really simple playground to replicate it (less than 10 lines of actual code): https://github.com/peterbakonyi05/winston-log-rotate-reconfiguration-issue/blob/master/index.js

What do you expect to happen instead?

winston creates a new stream and handles reconfiguration.

Note

Alternatively, instead of reconfiguring winston a new instance can be created, in case someone else has the same issue. Just call .close on the old instance and create a new from scratch.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:16
  • Comments:22 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
JadianZhengcommented, Jan 11, 2021

Still happen in 3.3.3

3reactions
apaparazzi0329commented, Jun 12, 2019

tl;dr Pass by reference in logger.add() function causes the transport object to be altered and thus invalidated when used again in logger.configure()

I think I found the cause of this issue, stemming from the logger.add() function:

add(transport) {
    
    console.log('In add function\n')
    const target =
      !isStream(transport) || transport.log.length > 2
        ? new LegacyTransportStream({ transport })
        : transport;
  
    console.log("Original target:", JSON.stringify(target), "\n")
    if (!target._writableState || !target._writableState.objectMode) {
      throw new Error(
        'Transports must WritableStreams in objectMode. Set { objectMode: true }.'
      );
    }

    // Listen for the `error` event and the `warn` event on the new Transport.
    this._onEvent('error', target);
    this._onEvent('warn', target);
    this.pipe(target);

    console.log("Target after pipe:", JSON.stringify(target))

    if (transport.handleExceptions) {
      this.exceptions.handle();
    }

    if (transport.handleRejections) {
      this.rejections.handle();
    }

    return this;
  }

Screen Shot 2019-06-12 at 2 39 45 PM

I added my own print statements to keep track of the target object. As can be seen in output from the print statements; the target object is edited after it is used for piping. Since target is equivalent to the transport parameter through pass by reference, the change to target also changes the transport object and thus makes it invalid for reuse in logger.configure(). This explains why an entirely new but identical transport would work with configure() but editing the original transport would give the error.

Probable Solution: Make a deep copy of the passed in transport object so that the target object is pass by value

This is my first time commenting on an open issue on GitHub so please let me know if I missed anything that would make this more helpful. I would have tried to create a coded solution myself but since object cloning doesn’t seem to be a native tool in JavaScript I figured it would be better for someone with more knowledge of the code base to create an actual solution

Read more comments on GitHub >

github_iconTop Results From Across the Web

Designing Error Messages and a Logging Strategy in Node.js
Learn how to structure helpful error messages and follow a good logging strategy.
Read more >
"write after end" error in a server made with nodejs
The problem is that when I try to test it, it always crashes the server with an "error: write after end". Can anyone...
Read more >
A Complete Guide to Winston Logging in Node.js - Better Stack
Learn how to start logging with Winston in Node.js and go from basics to best practices in no time.
Read more >
Access and Error Logs - The Ultimate Guide To Logging - Loggly
Access and Error Logs. Log Files. An Apache log is a record of the events that have occurred on your Apache web server....
Read more >
tslog - Extensible TypeScript Logger for Node.js and Browser ...
Extensible TypeScript Logger for Node.js and Browser: Dependency free, Fully customizable, Pretty errors, stack traces, and JSON output to attachable ...
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