2

I'm starting my node.js server like this:

sudo NODE_ENV=production forever start -o out.log -e err.log server.js

Occasionally I get errors in my error log.

But all I get is something like this:

Cannot read property 'length' of undefined
    at eval at <anonymous> (/home/ubuntu/www/node_modules/jade/lib/jade.js:161:8)
    at anonymous (eval at <anonymous> (/home/ubuntu/www/node_modules/jade/lib/jade.js:161:8))
    at Object.<anonymous> (/home/ubuntu/www/node_modules/jade/lib/jade.js:166:12)
    at ServerResponse._render (/home/ubuntu/www/node_modules/express/lib/view.js:422:21)
    at ServerResponse.render (/home/ubuntu/www/node_modules/express/lib/view.js:315:17)
    at callbacks (/home/ubuntu/www/node_modules/express/lib/router/index.js:272:11)
    at Promise.<anonymous> (/home/ubuntu/www/helper/auth.js:118:11)
    at Promise.<anonymous> (/home/ubuntu/www/node_modules/mongoose/lib/promise.js:120:8)

How do I make the error log display more information, such as the datetime the error took place?

2
  • I think that maybe your jade version is outdated - what does npm ls say? Commented Nov 1, 2011 at 17:10
  • @thejh just an example, not actually trying to debug the err, thanks though Commented Nov 2, 2011 at 8:51

2 Answers 2

3

use the 'uncaughtException' Event like this:

process.on('uncaughtException', function (err) {
  console.log('Caught exception: ' + err);
});

setTimeout(function () {
  console.log('This will still run.');
}, 500);

// Intentionally cause an exception, but don't catch it.
nonexistentFunc();
console.log('This will not run.');

than you could log a date and whatever else you want to log out in the callback.

more on process here: http://nodejs.org/docs/v0.4.12/api/process.html#event_uncaughtException_

Sign up to request clarification or add additional context in comments.

1 Comment

how do I make it log a stacktrace?
1

If you want datetime information, you can print using require('util').log()

If you also want to prevent your application from exiting, you can catch uncaught exceptions

var util = require('util');

process.on('uncaughtException', function (err) {
//Traceout the error details    
console.error(err.stack)
  util.log('Caught exception: ' + err.message);
});

Keep in mind that if an exception gets thrown, the callback sequence that may have been running is stopped, so your application may have strange behavior if you continue to run it. It might be wise to add a process.exit() into the uncaught exception handler above.

Comments

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.