Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 1000
matanster Avatar asked Nov 11 '22 04:11

matanster


1 Answers

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.

like image 194
Artem Ibragimov Avatar answered Dec 18 '22 13:12

Artem Ibragimov