zwave-js / zwave-js-ui

Full featured Z-Wave Control Panel UI and MQTT gateway. Built using Nodejs, and Vue/Vuetify
https://zwave-js.github.io/zwave-js-ui
MIT License
985 stars 201 forks source link

fix: re-use file transport instance when setup loggers #3928

Closed robertsLando closed 1 month ago

robertsLando commented 1 month ago

Fixes #2937

coveralls commented 1 month ago

Pull Request Test Coverage Report for Build 11382649159

Details


Files with Coverage Reduction New Missed Lines %
api/lib/logger.ts 2 89.15%
<!-- Total: 2 -->
Totals Coverage Status
Change from base Build 11382077088: 0.1%
Covered Lines: 3946
Relevant Lines: 19889

💛 - Coveralls
spudwebb commented 1 month ago

I haven't tested yet, but what about other transports? Shouldn't console and stream transports be created only once as well?

robertsLando commented 1 month ago

@spudwebb good point, I focused on the filetransport but it makes sense to extend this to all transports list. Just submitted the fix

spudwebb commented 1 month ago

With this branch I now get the followings warnings in the console when I start ZUI

(node:21720) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [Stream]. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
(node:21720) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 unpipe listeners added to [DailyRotateFile]. Use emitter.setMaxListeners() to increase limit
(node:21720) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [DailyRotateFile]. Use emitter.setMaxListeners() to increase limit

Here is what I get when I use --trace-warnings:

(node:48884) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 unpipe listeners added to [DailyRotateFile]. Use emitter.setMaxListeners() to increase limit
    at genericNodeError (node:internal/errors:984:15)
    at wrappedFn (node:internal/errors:538:14)
    at _addListener (node:events:592:17)
    at DailyRotateFile.addListener (node:events:610:10)
    at DerivedLogger.Readable.pipe (C:\temp\zwave-js-ui\node_modules\winston\node_modules\readable-stream\lib\_stream_readable.js:584:8)
    at DerivedLogger.add (C:\temp\zwave-js-ui\node_modules\winston\lib\winston\logger.js:364:10)
    at C:\temp\zwave-js-ui\node_modules\winston\lib\winston\logger.js:124:44
    at Array.forEach (<anonymous>)
    at DerivedLogger.configure (C:\temp\zwave-js-ui\node_modules\winston\lib\winston\logger.js:124:18)
    at setupLogger (C:\temp\zwave-js-ui\server\lib\logger.js:167:12)
spudwebb commented 1 month ago

@robertsLando I confirm that with this branch the original error I got at midnight when the files rotate is gone, but as stated above I now get a MaxListenersExceededWarningnode.js warning everytime I start.

robertsLando commented 1 month ago

if I detect that a transport needs to be recreated, I first destroy them: https://github.com/zwave-js/node-zwave-js/blob/37629fa0934b654d81b6c0e74ce6709dc0b61532/packages/core/src/log/shared.ts#L132-L143 and then create a new instance if necessary: https://github.com/zwave-js/node-zwave-js/blob/37629fa0934b654d81b6c0e74ce6709dc0b61532/packages/core/src/log/shared.ts#L206-L208 finally, I re-configure the log container: https://github.com/zwave-js/node-zwave-js/blob/37629fa0934b654d81b6c0e74ce6709dc0b61532/packages/core/src/log/shared.ts#L148

@spudwebb I will fix this asap, give me some time as I'm very full in this period

spudwebb commented 1 month ago

@robertsLando I'm not sure there is anything wrong with the way you implemented this. These warnings appear because of the number of module loggers in ZUI which now triggers the default 10 limit on event listeners on Streamtransport and DailyRotateFiletransport. I saw that you can get rid of these warnings by increasing the number of max listeners, which seems to be what is done in the Console transport: https://github.com/search?q=repo%3Awinstonjs%2Fwinston%20setMaxListeners&type=code

So if I do the same thing in the customTransportsfunction for streamTransportand fileTransportthen the warnings are gone.

    streamTransport.setMaxListeners(30)
    transportsList.push(streamTransport)
     fileTransport.setMaxListeners(30)
         transportsList.push(fileTransport)
robertsLando commented 1 month ago

@spudwebb Sorry, just found some time to look at this again, release is coming. Thanks again for your help 🙏🏼