Harry
Harry

Reputation: 54999

node.js error log include more information

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?

Upvotes: 2

Views: 4232

Answers (2)

geoffreak
geoffreak

Reputation: 2328

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.

Upvotes: 1

Chris Biscardi
Chris Biscardi

Reputation: 3153

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_

Upvotes: 3

Related Questions