Search code examples
google-app-engineexpressgoogle-cloud-platformstackdrivergoogle-cloud-error-reporting

Using Error report of Google Cloud Platform with ExpressJS


I switched my ExpressJS application from Heroku to Google App Engine.
Everything works ok now.
But I am curious about how to debug my application if some exceptions happen on GAE.
On heroku, I can do heroku logs -t to trace the errors. I can check the variable printed by console.error(var) as well.
However, I don't know how to do the same thing on GAE.

I have checked logging of Stack Driver, it seems that it only shows some informations of every HTTP request instead of the detailed logs like heroku.
I have found that there is error report service of Stack Driver. It may what I want.
Here is the tutorial telling us how to setup. But the steps are confusing for me.
Does anyone have the experience to setup the error reporting?
I am finding a more clear steps to setup this.
Thanks a lot and appreciated!


Solution

  • For Stackdriver Error Reporting:

    You can use code similar to the one advised for Google Compute Engine: https://cloud.google.com/error-reporting/docs/setup/compute-engine#send_exception_data

    Here is what worked for me using Express and Winston on App Engine flexible environment:

    var winston = require('winston');
    winston.add(winston.transports.File, { filename: '/var/log/app_engine/custom_logs/my.errors.json' });
    
    var report = function (err, req) {
      var payload = {
        serviceContext: {
          service: 'my service',
        },
        message: err.stack,
        context: {
          httpRequest: {
            url: req.originalUrl,
            method: req.method,
            referrer: req.header('Referer'),
            userAgent: req.header('User-Agent'),
            remoteIp: req.ip,
            responseStatusCode: 500,
          }
        }
      };
      winston.error (payload);
    };
    
    // Handle errors
    app.use(function (err, req, res, next) {
      report(err, req);
      res.status(500).send(err.response || 'Something broke!');
    });
    

    For Stackdriver Logging:

    Indeed, the request_log only contains HTTP requests log entries on App Engine flex. Look in the stdout log to see the output of your application.