-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ExceptionHandler silences printing of console errors? #1289
Comments
As one comment, in theory you can control whether the exception causes an exit or not by setting |
Looked a bit further and found this in exception-handler.js asyncForEach(handlers, function awaitLog(handler, next) {
//
// TODO: Change these to the correct WritableStream events
// so that we wait until exit.
//
var done = once(next);
var transport = handler.transport || handler;
//
// Debug wrapping so that we can inspect what's going on
// under the covers.
//
function onDone(event) {
return function () {
debug(event);
done();
};
}
transport.once('logged', onDone('logged'));
transport.once('error', onDone('error'));
}, gracefulExit); Seems like multiple issues here:
So, issue confirmed, and there's some work to be done to get this all in order. Thanks for bringing it up! (PRs welcome of course if you're interested -- otherwise I'll try to find time to fix this up) |
Even with ^^that little lambda as the callback and |
So, did some more digging/learning and think I got some clarity on this. First of all, https://nodejs.org/api/process.html#process_event_uncaughtexception . Uncaught exceptions are unsafe and it's not good / unsafe to attempt to resume normal execution after one of these. Instead, the handling of uncaught exceptions should be thought of as doing some last-minute logging/cleanup before the process terminates. This isn't clearly documented, but it appears that Node will terminate the process after the Given that, So I believe we are seeing the expected behavior here, i.e. we see one uncaught exception logged, and then the process terminates. Seems like this behavior is basically enforced by Node. Nonetheless, this warrants:
|
This is somewhat a duplicate of #1263, but there is more sublty. Look at this modification of your MWE (thanks for that btw): let winston = require('winston');
const path = require('path');
const PRODUCTION = false;
// LOGGING
const myFormat = winston.format.printf(info => {
return `${info.timestamp} ${info.level}: ${info.message}`;
});
//console.log(undefinedVariable); // THIS GIVES A REFERENCE ERROR
const logger = winston.createLogger({
level: 'debug',
format: winston.format.combine(winston.format.timestamp(), myFormat), // winston.format.json(),
transports: [
new winston.transports.File({filename: 'error.log', level: 'error'}),
new winston.transports.File({filename: 'combined.log'}),
],
exceptionHandlers: [
new winston.transports.File({ filename: 'exceptions.log' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
// console.log(undefinedVariable); // THIS DOES NOT GIVE A REFERENCE ERROR, BUT ENDS THE SCRIPT SILENTLY
if (!PRODUCTION) {
// If we're not in production then also log to the `console`
logger.add(new winston.transports.Console({
format: winston.format.combine(winston.format.timestamp(), myFormat),
level: 'debug',
handleExceptions: true
}));
}
setTimeout(() => {
console.log(undefinedVariable); // THIS ALSO DOES NOT GIVE A REFERENCE ERROR, BUT ENDS THE SCRIPT SILENTLY
}, 2000); I've done two things:
Now when I run this the log of the exception is written to all three locations (Console,
Thanks for all your help! We're going to release |
@indexzero I'd like to draw attention back to this issue from ~3 months ago. DABH's patch works (I've provided further tests in the PR to confirm this), but it sounds like DABH is looking for a confident verdict on it. This codebase is wholly unfamiliar to me (as are streams, etc.), so I appeal to core contributors such as yourself for the necessary expertise here. |
Fixed in #1355. Will be released in |
I think this needs more work. I came across this same issue with winston logging stopping when there is an unhandled exception. I was using an earlier version so I updated to 3.2.1 to get fix #1355 but it still not working as expected. I found the following two issues through extensive debugging.
This is caused by fix #1355 always setting transport._ending to true. As a work-around I changed it to assign doExit instead of true, but really the whole asyncForEach should probably not be run if doExit is false.
|
In my Winston logger I use an exceptionHandler so that the node exceptions/errors also reach my logs. I now have a problem though. When I run the script from the command line using
node index.js
(instead of pm2) the script ends silently when it has an error.Have a look at my Winston implementation below. I added three
console.log()
s which try to log an undefined variable. When I run the script usingnode index.js
it gives me aReferenceError
for the first wrongconsole.log(undefinedVariable)
as expected. When I now remove that first and/or the secondconsole.log
, the script ends silently.I'm running version
3.0.0-rc5
on Ubuntu 18.04.Does anybody know what I'm doing wrong here? All tips are welcome!
The text was updated successfully, but these errors were encountered: