log4js-node / log4js-node

A port of log4js to node.js
https://log4js-node.github.io/log4js-node/
Other
5.8k stars 773 forks source link

log4js.fileAppender - Writing to file .../server.log, error happened #1114

Open fredakoe opened 2 years ago

fredakoe commented 2 years ago

Hello All,

First, Happy new year to all. I'am facing issues working with log4js@4.5.1 when disk is full. My error is : log4js.fileAppender - Writing to file [...]/server.log, error happened { Error: ENOSPC: no space left on device, write at Error (native) errno: -28, code: 'ENOSPC', syscall: 'write' } I want to catch "ENOSPC" error and do some actions. In my code, i added a "uncaughtException" listener and do some stuff when "ENOSPC" error triggers. But when the ENOSPC triggers, my listener is never called! can anyone helps me in what to properly catch that error from log4js please?

Regards.

lamweili commented 2 years ago

Hi, would you mind providing your code snippet?

fredakoe commented 2 years ago

Hello,

Thanks for the reply. below are snippsets:

1- ### logger.js file var path = require('path'); var log4js = require('log4js'); log4js.configure(path.join(__dirname, '..', 'settings/log4js.json')); exports.server = log4js.getLogger('server');

2- ### log4js.json file { "appenders": { "multi": { "type": "multiFile", "layout": { "type": "coloured" }, "base": my_log_directory, "property": "categoryName", "extension": ".log" } }, "categories": { "default": { "appenders": ["multi"], "level": "ALL" } } }

3- ### my main code var logger = require('logger').server; process.on('uncaughtException', function (err, origin) { switch(err.code) { case 'ENOSPC': doSomeStuff() break; default: break; } });

i use logger (when logging) like this: logger.[level_i_want]("lorem ipsum ...");

Thanks for any help.

Regards.

fredakoe commented 2 years ago

Hello all,

No one to help me? :-(

Regards.

lamweili commented 2 years ago

If an EventEmitter does not have at least one listener registered for the 'error' event, and an 'error' event is emitted, the error is thrown, a stack trace is printed, and the Node.js process exits. (src: https://nodejs.org/docs/latest/api/events.html#error-events)

Currently, the code has an 'error' listener, which does the logging that you see. Thus, the error will not be thrown to uncaughtException which explains why your listener is not receiving any events.

https://github.com/log4js-node/log4js-node/blob/c9d67604c0fd4526925732cfde782a392f631d8d/lib/appenders/file.js#L23-L25

I'm not exactly sure how this can be achieved and this is a duplicate of issue #575.

Originally posted by @nomiddlename in #575 (comment) Hi, there isn't a way to do that right now - I/O errors mostly just console.error out (which is what the file appender does) or they bubble up as uncaught exceptions. In your situation though, it sounds like waiting until you get a "disk is full" error is probably too late. It may make more sense to just poll the disk periodically and warn the user when it looks like it's getting full?

I'd be happy to see a pull request that comes up with a way to handle appender errors across the board - I wouldn't want each appender to be doing something different.

lamweili commented 2 years ago

Maybe we should emit some sort of events within log4js instead of silently logging, to provide an option for developers who wish to listen/handle such events.