dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.72k stars 166 forks source link

Adding log to API Gateway request and response #112

Closed jilseph closed 5 years ago

jilseph commented 5 years ago

Log API Gateway requests and responses for further analysis in CloudWatch Logs Insights.

dougmoscrop commented 5 years ago

Other than the event being the 'cleaned up' event, this is just logging the event/context params and the response object; this can be achieved in any relevant code by:

const serverless = require('serverless-http');

const app = ...
const handle = serverless(app);

module.exports.handler = async (event, context) {
  yourLogger.log(event, context);
  const response = await handle(event, context);
  yourLogger.log(response);

  return response;
};

This can get even simpler if you combine with some of the middleware-style features of @alpha-lambda, i.e. it can become:

const serverless = require('serverless-http');
const handler = require('@alpha-lambda/handler');
const logging = require('e.g.logging-middleware') // we have one of these, but not released to public yet, we should soon

const app = ...

module.exports.handler = handler()
  .use(logging())
  .use(serverless(app));
};

I'm not super keen on including this console.log behaviour as part of the core library.

dougmoscrop commented 5 years ago

Closing as I believe the logging issue can be addressed via middleware/regular functions, not part of this library