lambda-api icon indicating copy to clipboard operation
lambda-api copied to clipboard

Logging to Loggly

Open JustinLutsky opened this issue 5 years ago • 2 comments
trafficstars

Is it possible to pipe all logging entries to Loggly? Any kind of logging middleware that can be added, including access logs?

JustinLutsky avatar Feb 13 '20 06:02 JustinLutsky

This library looked apt and so good until we ran into this issue where the logger tightly integrated with req and response object without being exposed for extension or wrapping. Hence, you cannot even wrap it for scenarios when you might want to move away from cloudwatch. The library has assumed a stack and tailored code for it.

navneetthakur99 avatar Feb 09 '21 15:02 navneetthakur99

Hi, Lambda API is optimised to execute on AWS Lambda and within its ecosystem including CloudWatch. That said there's at least four ways you can attempt integrating with external logging systems.

1. Lambda API Middleware You can console.log additional information or send it externally using middleware for every request. The latter will likely make your execution time longer, but is fairly straightforward:

api.use((req,res,next) => {
  // log req data or send it externally...
  next()
})

2. Wrapper inside the Lambda handler If you need to wrap the whole request and response, you can always do it in your Lambda handler. Simply avoid returning the Lambda API run's results immediately. This is a handy way of using tools like: https://github.com/awslabs/aws-embedded-metrics-node

exports.handler = async (event, context) => {
  // Perform actions before request processed...
 const result = await api.run(event, context)
 // Perform actions after request processed...
 return result;
}

3. Lambda Extensions The methods above involve running code as part of your function's invocation request. If you want to avoid doing that, you can try Lambda Extensions, which allow you to separate collection and transmission of metrics or logs from your main function. There are also existing extensions you can use for services like: Datadog, Dynatrace, New Relic, so you don't have to roll your own and it will allow you to disable CloudWatch: https://aws.amazon.com/blogs/compute/introducing-aws-lambda-extensions-in-preview/ (Unfortunately, I haven't seen one for Loggly)

4. Stream from CloudWatch Finally, you can always transmit your logs from CloudWatch to a different service by subscribing to your log streams. Below is a tutorial from Loggly: https://www.loggly.com/blog/sending-aws-cloudwatch-logs-to-loggly-with-aws-lambda/

Hope this helps!

stawecki avatar Feb 09 '21 16:02 stawecki