Logging for Lambda
Based on Structlog.
Structured and enriched logging for AWS Lambda functions.
TL;DR:
- Decorate handler with
logging_wrapper - Encrich logs with key/value pairs using
log_add - Time functions with
log_duration - Log exceptions with
log_exception
Usage
Wrap your lambda handler with logging_wrapper. Badabing badabom, you're good to go!
You can set the service name using the logging.init method, or configure it using
the SERVICE_NAME environment variable.
from dataplatform.awslambda import logging
from dataplatform.awslambda.logging import logging_wrapper
logging.init("my_fantastic_lambda")
@logging_wrapper
def handler(event, context):
if error:
return {
"statusCode": 500,
"body": "Automatically logs bodies from error responses even!",
}
Encriching logs
By automagic logs will be enriched with git revisions, cold start y/n call
duration and much more, but to add even more magic you can use log_add and
log_duration.
from dataplatform.awslambda.logging import logging_wrapper, log_add, log_duration
@logging_wrapper
def handler(event, context):
log_add(dataset=event["dataset"], foo=context["foo"])
log_duration(
lambda: slow_thinger(event["dataset"]),
"my_slow_thinger"
)
... and so on
def slow_thinger():
sleep(9999999999999999)
Exceptions
Struct log can extract exception info if we log the exception to the special
exc_info
key.
For convenience we catch and log uncaught exceptions using this already.
If you need to process an exception you can use log_exception to log it to
the exc_info key.
from dataplatform.awslambda.logging import logging_wrapper, log_exception
@logging_wrapper
def handler(event, context):
try:
thing()
except MyException as e:
log_exception(e)
return { ... }
Metadata
Release files for origo-lambda-helpers 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| origo-lambda-helpers-0.1.0.tar.gz | 4.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| origo_lambda_helpers-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.2 kB
Release files / origo-lambda-helpers-0.1.0.tar.gz
| Download URL | origo-lambda-helpers-0.1.0.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
62d692ec8b399429c90476d37b9278d30ffc9bbe8576b4d6fa25262a7eb98aca
|
|
BLAKE2b-256 checksum How to use checksums |
468e07b80745ab7ee24510f0115402c2571f502038f26a3dceaca2747ad202bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5
|
Release files / origo_lambda_helpers-0.1.0-py3-none-any.whl
| Download URL | origo_lambda_helpers-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f2d9c5f6c25cf7114d670a8546f31177086804de2310696f96179ec94ad8a9db
|
|
BLAKE2b-256 checksum How to use checksums |
028b0a24b5a5a044d6e4a867e591eb80369177130b575edabbe01a6279f8829e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5
|