title: Best practice for node.js asynchronous logging and unhandled server exceptions
body: <p>We recently changed our logging code in node.js/Express.js to write to the console asynchronously. However, this comes with an issue where if after the asynchronous code is initiated (and when it runs it will emit a log message), the synchronous code run after that initiation crashes the server, then the logs are never written.</p>
  
  <p>Is there an established pattern or best practice for handling this so that disk I/O is not a blocker for logging, but in the event of an unhandled exception (such as an exception occurring in our last-chance exception handler in Express.js), the logs will still be written to disk/console?</p>
  
  <p>I thought about starting a separate logging process and doing inter-process communication, but that feels pretty darn heavy just to avoid a little bit of I/O. And I don't think a separate thread will work—won't the node.js unhandled exception that takes the server down also kill all threads started/owned by the process? (I'm no expert in this area.)</p>
  
  <p>Or is it better to just write to the console synchronously in our logger, and thereby prevent this situation in the first place?</p>