simple-node-logger icon indicating copy to clipboard operation
simple-node-logger copied to clipboard

log.error() will throw following exception

Open jordy-leung opened this issue 7 years ago • 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)

jordy-leung avatar Jan 25 '18 08:01 jordy-leung

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

darrylwest avatar Jan 29 '18 21:01 darrylwest

Im getting the same error . My node version is 8.9.4

clive-school24 avatar Feb 02 '18 10:02 clive-school24

I'm not able to reproduce so can you send me a snippet of code that breaks?

darrylwest avatar Feb 03 '18 18:02 darrylwest

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");

zadam avatar Apr 02 '18 02:04 zadam

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 avatar Apr 04 '18 06:04 bioleyl

@bioleyl Thanks for the fix! Its a problem on macOS as well.

vinczebalazs avatar Aug 01 '18 11:08 vinczebalazs

FYI

I am having the problem on Ubuntu 18.04 / Node v8.10.0

Thanks for the fix

Wayn0 avatar Sep 05 '18 14:09 Wayn0

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.

https://www.npmjs.com/package/sw-node-logger

bioleyl avatar Sep 09 '18 18:09 bioleyl

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) {

jpgri avatar Oct 18 '18 13:10 jpgri

I experience this behavior even with the latest version (18.12.21) that should have the commit that fixed the issue. Is it correct?

ghost avatar Jan 15 '19 19:01 ghost

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.

darrylwest avatar Jan 15 '19 21:01 darrylwest

Yes, Windows 7 64-bit. log.fatal() and the other methods work fine.

ghost avatar Jan 16 '19 08:01 ghost

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)

pow4all avatar Apr 04 '19 20:04 pow4all

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 avatar Apr 22 '19 14:04 benivalotker

@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().

kyletuite avatar Feb 20 '20 17:02 kyletuite

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");

thanhtruong1992 avatar Jun 18 '20 09:06 thanhtruong1992

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.

awps avatar Jun 07 '21 12:06 awps

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 );

saru2020 avatar Nov 13 '21 18:11 saru2020

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...

jan-kacina avatar Mar 03 '22 13:03 jan-kacina

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.

pablo-penovi avatar Apr 23 '22 20:04 pablo-penovi