title: NodeJS ExpressJS - how to log errors for the production server?
body: <p>How can I log any errors from ExpressJS apps into a file?</p>
  
  <p>I know I can log a Slim framework easily with <a href="https://github.com/Seldaek/monolog" rel="noreferrer">monolog</a>:</p>
  
  <pre><code>$app-&gt;get('/tickets', function (Request $request, Response $response) {
      $this-&gt;logger-&gt;addInfo("Something interesting happened");
  
      $mapper = new Simon\TicketMapper($this-&gt;db);
      $tickets = $mapper-&gt;getTickets();
  
      $response-&gt;getBody()-&gt;write(var_export($tickets, true));
      return $response;
  });
  </code></pre>
  
  <p>In Express, I usually log the error to console for <strong>development</strong>:</p>
  
  <pre><code>Model.findOne(options, function(err, doc) {
  
      var output = {};
      if (err) {
          console.log("Error retrieving doc: " + err);
      }
  </code></pre>
  
  <p>But in my <strong>production</strong> server, I do need a helpful log file when the app goes wrong.</p>
  
  <p>Any ideas?</p>
  
  <p>In Express's <code>app.js</code>, it has this:</p>
  
  <pre><code>// development error handler
  // will print stacktrace
  if (app.get('env') === 'development') {
  
    app.use(function(err, req, res, next) {
      res.status(err.status || 500);
      res.render('error', {
          message: err.message,
          error: err
      });
    });
  
  }
  
  // production error handler
  // no stacktraces leaked to user
  app.use(function(err, req, res, next) {
      res.status(err.status || 500);
      res.render('error', {
          message: err.message,
          error: {}
      });
  });
  </code></pre>
  
  <p>But how do I turn the production's one on? Is it the same as monolog that I need?</p>