title: Node JS - Log worker cluster last exception
body: <p>I'm working on Node JS server (0.10.30) along with its Cluster feature.
  Each time a worker terminates I catch the 'exit' event and restarting a new worker. 
  I would like to also log (on the master cluster) the reason that worker was terminated, e.g. an Exception or a Fatal Error.</p>
  
  <p>How to do that?</p>
  
  <p>My app.js file looks something like that:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var cluster = require('cluster');
  var numCPUs = 2;
  
  if (cluster.isMaster) {
    for (var i = 0; i &lt; numCPUs; i++) {
      cluster.fork();
    } 
    
    cluster.on('exit', function(worker, code, signal) {
   console.log('worker %d died. Starting a new worker:', worker.id);
   cluster.fork();
  });
  
  } else {
    var domain = require('domain');
    var d = domain.create();
    d.on('error', function(er) {
      console.error('error: ', er.stack);
      // I never get the memory fatal error here 
    });
  
    d.run(function() {
      // The memory runs out in an async call
    });
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Thanks,
  Erez</p>