Logging configuration to make debugging Lambdas easier
Project description
lambda-debug-logging
lambda-debug-logging is a Python library that helps reduce logging costs without sacrificing debugging capabilities. The library will automatically detect when your Lambda execution as failed. If the execution failed, then all of your debug logs will get written out to CloudWatch. If your execution succeeded, then only a small percentage of your debug logs will get written out to CloudWatch.
Installation
$ pip install lambda-debug-logging
Usage
from lambda_debug_logging import lambda_debug_logging, register_handler
register_handler()
@lambda_debug_logging()
def handler(event, context):
return "success!"
Failure Detection
In many scenarios, the Lambda will succeed, but what happened during the execution should be considered a failure. A great example of that is the scenario where your Lambda is processing an HTTP request. A lambda that successfully sends back a 500 should still be considered a failure. The debug logs should get sent to CloudWatch in that scenario.
HTTP requests
When using a Lambda with an ALB or API Gateway, you can use the http_status_code_check
function to check for status codes >= 400.
from lambda_debug_logging import lambda_debug_logging, failure_detection, register_handler
register_handler()
@lambda_debug_logging(response_failure_check=failure_detection.http_status_code_check)
def handler(event, context):
return {
"statusCode": 500
}
API Gateway Auth Policy
When using a Lambda as an API Gateway custom authorizer, you can use the apigw_authpolicy_check
function to check for Access Denied polices.
from lambda_debug_logging import lambda_debug_logging, failure_detection, register_handler
register_handler()
@lambda_debug_logging(response_failure_check=failure_detection.apigw_authpolicy_check)
def handler(event, context):
return {
"policyDocument": {
"Statement": [
{
"Effect": "Deny"
}
]
}
}
Caveats
Out of order logs
Log messages that use INFO or above will log in realtime. Log messages that use DEBUG will be buffered in memory until later. When the library determines that DEBUG logs do need to be written out, they will be flushed from memory in bulk. This means the DEBUG logs will show up after the INFO logs. The default JSON Formatter includes a timestamp that allows you to re-sort the messages to be in order, but that is an extra step you have to do.
Filling the buffer
If your Lambda writes enough log messages, then the in-memory buffer will fill up. Rather than lose those logs, the library will flush those logs out.
Lambda timeouts
The DEBUG logs will not get written when the Lambda times out.
Why DEBUG logs are written
There are multiple reasons that DEBUG logs will get written
- The Lambda throw an exception
- The Lambda response was detected as having a failure response
- log.error() was called
- clear_buffer() was called (based on the sample rate)
- The Lambda succeeded (based on the sample rate)
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
Dependencies
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file lambda-debug-logging-0.1.0.tar.gz
.
File metadata
- Download URL: lambda-debug-logging-0.1.0.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | daffc897b3bbdeb55760594da9d59a3bdc8455b80ddfe80245acb5eddc57eee3 |
|
MD5 | a3ab7b403189768c0172b38610b97624 |
|
BLAKE2b-256 | fc95b3dbd8446c4dbbc2b4a45ebb888bdf09ba9400fefa9d879a8ea1f76475ac |