log.error() will throw following exception
Opened this issue ยท 20 comments
Hi,
I just start to use simple-node-logger. When I use "log.error()", I will get this exception. Do you know what I have done incorrectly? It runs ok with log.debug or log.info, etc ๐
Would you point me right direction? Thanks!
=============================================
events.js:188
throw err;
^
Error: Unhandled "error" event. ([object Object])
at process.emit (events.js:186:19)
at D:\DEV\nodejs_projects\SystemMonitors\node_modules\simple-node-logger\lib\Logger.js:46:25
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Hi Jordy,
This is a new one to me. Can you tell me what node version you are running and supply some sample code? Also, try running the examples to see if they run ok.
thanks, darryl
Im getting the same error . My node version is 8.9.4
I'm not able to reproduce so can you send me a snippet of code that breaks?
Same for me on node 8.11.1:
const logger = require('simple-node-logger').createRollingFileLogger({
errorEventName: 'error',
logDirectory: 'app/logs',
fileNamePattern: 'app-<DATE>.log',
dateFormat:'YYYY-MM-DD'
});
logger.error("This throws exception");
This is a problem on Windows.
To fix it, replace the line 45 of Logger.js by this one:
if (level === 'error' && typeof(errorEventName) === String) {
I created a PR #31
@bioleyl Thanks for the fix! Its a problem on macOS as well.
FYI
I am having the problem on Ubuntu 18.04 / Node v8.10.0
Thanks for the fix
There is not much activity on the sources.
Anyway I needed syslog function and auto remove (old) logfiles. For now if you remove old log when the app is running, it will crash. If you are interested to test, I made a really simple package to manage the log files. Give it a try and send me back if something is missing for you.
This solves the problem but it no make sense because String is not a possible output for typeof().
Putting
if (level === 'error' && typeof(errorEventName) === String) {
is exactly the same as putting:
if (false) {
I experience this behavior even with the latest version (18.12.21) that should have the commit that fixed the issue. Is it correct?
hi Mark, are you on Windows? I haven't been able to reproduce this error on mac or linux but I don't have a windows env to test. I guess I should create a windows docker image to see if I can reproduce.
Yes, Windows 7 64-bit. log.fatal()
and the other methods work fine.
I am on Ubuntu 18.10 with Node 8.11.4 and log.error()
throws also this exception.
Edit:
simple-node-logger version is 18.12.22
The exact error message is:
events.js:188
throw err;
^
Error: Unhandled "error" event. ([object Object])
at process.emit (events.js:186:19)
at /home/<xxx>/node_modules/simple-node-logger/lib/Logger.js:46:25
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
because this line: "errorEventName:'error' " - throw error in node.
remove this line from your "simple-node-looger" config object and the error will not show anymore.
@benivalotker I know this is almost a year later, but this is the fix that worked for me. Thank you! I was having this issue using log.error()
after switching from createSimpleFileLogger()
to createRollingFileLogger()
.
Fixed it!
const logger = require('simple-node-logger').createRollingFileLogger({
logDirectory: 'path_name/logs',
fileNamePattern: 'app-<DATE>.log',
dateFormat:'YYYY-MM-DD'
});
logger.log("error", "This throws exception");
The problem is still not fixed as I see:
events.js:305
throw err; // Unhandled 'error' event
^
Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({
ts: 1623069318792,
pid: 27492,
level: 'error',
msg: [ 'My message' ]
})
at process.emit (events.js:303:17)
at process.EventEmitter.emit (domain.js:483:12)
at /home/andrew/project/node-app/node_modules/simple-node-logger/lib/Logger.js:46:25
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
code: 'ERR_UNHANDLED_ERROR',
context: {
ts: 1623069318792,
pid: 27492,
level: 'error',
msg: [ 'My message' ]
}
}
It disappears if I remove this line from options errorEventName: 'error',
, but this looks like it's a bug.
After lots of struggle, finally, I simply followed the examples and got it working.
const opts = {
domain:'Domain-A',
logDirectory: __dirname + '../../../logfiles',
fileNamePattern: 'hourly-test-.log',
dateFormat:'YYYY.MM.DD-HH'
};
const log = require('simple-node-logger').createRollingFileLogger( opts );
The same error here:
- Ubuntu 20.04.4
- Node 16.14.0
- Simple Node Logger 21.8.12
What's the meaning of the code guarded by conditions? Based on previous comments and my case It looks like lines 45-47 could be removed completely...
Running into the same bug, couldn't figure out for the life of me where the exception was coming from, until I replaced ".error()" with ".info()" and voila, the exception disappeared. The exception I get is identical to the one @awps posted. What's more baffling is that there are other sections of the program where .error() works just fine.
Node: v16.9.1
OS: Ubuntu 22.04.
Simple Node Logger: 21.8.12
Since this program has a lot of async functions in it, I'm thinking maybe the problem stems from something related to async? I don't know really. I haven't been able to produce a minimal example of the bug nor can I show you the real source code since it's proprietary.