1

I am getting below error and I also know that it is throwing because the server.log file does not exist.

But what I want to know is how to handle this error programmatically like we do for unCaughtException e.g

process.on('unCaughtException', function(err){
    logger.error(err1)
})


Below is the error, I am getting:

events.js:141
  throw er; // Unhandled 'error' event
  ^

Error: ENOTDIR: not a directory, stat '../logs/server.log'
at Error (native)

Below is the actual code ..that is throwing this error.

module.exports = new winston.Logger({
transports: [
new winston.transports.File({
  level: 'info',
  filename: '../logs/server.log',
  json: false,
  maxsize: 5242880, //5MB
  maxFiles: 2,
  colorize: false
}),
new winston.transports.Console({
  level: 'debug',
  json: false,
  colorize: true
})
],
exitOnError: false
});
1
  • unCaughtException event name is wrong, uncaughtException is the right name. (this is a comment, not an aswer) Commented Apr 25, 2018 at 11:11

1 Answer 1

1

Judging by your code, you're using an event emitter that is emitting an error event when errors happen, in which case you need to listen for those events:

someLibrary({ logfile : '../logs/server.log' }).on('error', function(err) {
  ...handle the error...
});

More info here.

EDIT: in the specific situation of Winston, you should attach an error event handler to the transport instance:

module.exports = new winston.Logger({
  transports: [
    new winston.transports.File({
      level: 'info',
      filename: '../logs/server.log',
      json: false,
      maxsize: 5242880, //5MB
      maxFiles: 2,
      colorize: false
    }).on('error', function(err) {
      console.error(err.stack);
    }),
    new winston.transports.Console({
      level: 'debug',
      json: false,
      colorize: true
    })
  ],
  exitOnError: false
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks robert for quick suggestion...yes I am using winston moudle to configure logger
@Vikash check out this documentation about Winston-specifics (although I wonder if that applies to errors thrown by Winston itself).
I gone through the link u shared...will try for sure..but that looks more for handling exception ..not the error...is there not any other easy way to handle this...
In below link... github.com/winstonjs/… given, I tried ....logger.on('error', function (err) { /* Do Something */ }); but this did not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.