matanox
matanox

Reputation: 13706

node.js Winston - how to safely drain a logger

I have experimented with instantiating and closing winston loggers as (half) described on https://github.com/flatiron/winston#instantiating-your-own-logger, to no avail. I run into trouble closing file transports of Winston's - walking through it's source code, I found that the proper way to close off a logger would seem to be the close method. I expected this to take care of closing the transport file used by the logger - however that turned out to be not so.

Varying in frequency according to node.js server load, winston would still hold on to many transport files, infinitely long after the close method had been called for them, indefinitely long after no new writes were being initiated to them. I observed that through the node.js process file descriptors table (lsof -p). Even though close has been called for a Winston logger, it would indefinitely keep the file descriptor of the log file "in use", i.e. the log file never gets really closed. Thus leaking file descriptors and eventually making the node.js process bump into the ulimit (-n) limit after my application has been up for long.

Should there be a specific programming pattern for draining a Winston logger such that it can be eventually closed?

Upvotes: 6

Views: 5472

Answers (3)

Wernfried Domscheit
Wernfried Domscheit

Reputation: 59476

It's an old question, but according to documentation it would be like this:

logger.on('finish', function (info) {
  // All `info` log messages has now been logged
  // Do here whatever you like to do after last log message has been written.
});

logger.info('The last message');
logger.end();

Maybe add also

logger = null;

at the end of your script

Upvotes: 2

James
James

Reputation: 553

Create only one logger instance and then derive children from it. In this case, winston will hold only one open file handler. Might also be better for performance.

Upvotes: 1

Doron Zavelevsky
Doron Zavelevsky

Reputation: 1250

It's only a suggestion and I didn't try it myself - but I see you didn't get any other answers - so what's the harm...

It says here: https://github.com/flatiron/winston#file-transport

That you can provide a write stream instead of a file path. So why don't you manage the files yourself and when it's time to close - close the logger and then close the file handle?

Upvotes: 0

Related Questions