Skip to main content

SDK for origo

Project description

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 { ... }

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

origo-lambda-helpers-0.1.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

origo_lambda_helpers-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file origo-lambda-helpers-0.1.0.tar.gz.

File metadata

  • Download URL: origo-lambda-helpers-0.1.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for origo-lambda-helpers-0.1.0.tar.gz
Algorithm Hash digest
SHA256 62d692ec8b399429c90476d37b9278d30ffc9bbe8576b4d6fa25262a7eb98aca
MD5 62631e01086e716a97ff8b6764099ae9
BLAKE2b-256 468e07b80745ab7ee24510f0115402c2571f502038f26a3dceaca2747ad202bf

See more details on using hashes here.

File details

Details for the file origo_lambda_helpers-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: origo_lambda_helpers-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for origo_lambda_helpers-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2d9c5f6c25cf7114d670a8546f31177086804de2310696f96179ec94ad8a9db
MD5 d87a072d7b65b178de0da3fbd6166835
BLAKE2b-256 028b0a24b5a5a044d6e4a867e591eb80369177130b575edabbe01a6279f8829e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page