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.

Logging to file fails when calling process.exit(0)

See original GitHub issue

When you have winston configured to log to a file and you call process.exit(0) in your code, winston never ends up creating the log file and appending the log messages to the file.

In a perfect world, winston would happily handle this edge case and write to the file, but I’d also be fine with being able to call flush on a logger instance in order to manually force a flush to file.

There may be some way to manually ‘flush’ at the moment, but it’s not documented nor have I had any luck with manually to closing/ending/destroying a logger instance or its inner streams so far in a way that actually results in the loges being written out to a file…

var winston = require('winston');
var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)(),
    new (winston.transports.File)({ filename: 'winston.log' })
  ]
});

logger.info('info logged');
logger.error('error logged');

process.exit(0);
// the two logs get written out to console, but not to the winston.log file/

Issue Analytics

  • State:open
  • Created 10 years ago
  • Reactions:27
  • Comments:90 (8 by maintainers)

github_iconTop GitHub Comments

17reactions
3rd-Edencommented, Mar 13, 2015

I don’t really see this as a bug of winston anyways. When you process.exit in your code you have made the decision to completely kill everything that is still running on processing. If there are still HTTP requests running you don’t assume that these will also be replied to, so why would assume that log messages do end up in your log? This is just how node works things are async for a reason. If if you used the callback or setTimeout approach there is nothing preventing your application to log another line right before your setTimeout is done or callback is called.

The only ways around this is to have synchronous logging but then you defeat the whole purpose of having an async system as a simple log statement when you’re handling requests…

So what i’m suggesting is closing this issue with a wont fix. If you don’t want to mis any log messages make sure you correctly clean up and shut down your application before calling process.exit.

edit Also all your setTimeout hacks are flawed and do not fix the issue as you have no idea how long it will take to have all data be written to the operating system and how fast your disks or ssds’s are processing it. The only decent solution here is to listen to the stream’s drain event and exit the process. But if you application is continuously logging this might not even be called for a long period ;p making this just another hack because you didn’t properly shutdown your application before exiting.

12reactions
omeidcommented, Jul 16, 2019

@danielweck Nope, it is broken: #1629 #1504!

6 years, that is right, 6 years for a bug that is marked important. It is borderline impressive.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to force nodejs winston log to file before process exit
My OS is Ubuntu 16.04, Node 10.17. In the above case, the process will exit, but no log file will be created.
Read more >
Node exit logging | Pinecoder
This script fails, and two important things happen: It logs an error; It exits with a non-zero exit code. > node ./fail.js ...
Read more >
How to Fix 'Terminated With Exit Code 1' Error - Komodor
Exit Code 1 indicates that a container shut down, either because of an application failure or because the image pointed to an invalid...
Read more >
Let It Crash: Best Practices for Handling Node.js Errors on ...
Some strategies to gracefully shutdown the Node.js process and quickly ... we call process.exit and pass an argument of 0 because it is ......
Read more >
A Comprehensive Guide To Error Handling In Node.js
For example, the built-in JSON.parse() method throws an error if its ... and log the error for later assessment before exiting the process....
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