Skip to main content

An AWS approved version of https://github.com/TeoZosa/structlog-sentry-logger which is compatible with AWS Lambda as well as AWS Fargate

Project description

structlog-sentry-lambda

Description

This repository is a fork of https://github.com/TeoZosa/structlog-sentry-logger and all credits for this solution should go to @TeoZosa !!!

Use this ONLY if you are planning to integrate this into AWS Lambda or AWS Fargate.

For any other use-case, check the original repository.

Notes

Sentry SDK Initialization is NOT done within this library.

If you're going to use this library, you need to ensure that the Sentry SDK is properly initialized before importing the library.

Example for a standard AWS Lambda function:

filename = libraries/sentry.py

import os
import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration

sentry_sdk.init(
    dsn=os.environ.get('SENTRY_DSN'),
    integrations= [AwsLambdaIntegration(timeout_warning=True)],
    environment=os.environ.get('Workspace'),
    send_default_pii=True,
    traces_sample_rate=0.0,
    release=os.environ.get('SENTRY_RELEASE')
)

filename = libraries/logger.py

import simplejson as json
import structlog_sentry_lambda

SENTRY_LOGGER = structlog_sentry_lambda.get_logger()
logger = SENTRY_LOGGER.bind()


def debug(log_message):
    logger.debug(json.dumps(log_message, sort_keys=True, default=str))


def error(log_message):
    logger.error(json.dumps(log_message, sort_keys=True, default=str))


def exception(log_message):
    logger.exception(json.dumps(log_message, sort_keys=True, default=str))
    
    
def warning(log_message):
    logger.warning(json.dumps(log_message, sort_keys=True, default=str))


def info(log_message):
    logger.info(json.dumps(log_message, sort_keys=True, default=str))


def main(log_message):
    logger.info(json.dumps(log_message, sort_keys=True, default=str))


def log(log_message):
    logger.info(json.dumps(log_message, sort_keys=True, default=str))

filename = index.py

import sys
sys.path.append('../libraries/')
from libraries import sentry
from libraries import logger

def main(event, context):
    logger.info(event)
    logger.error("Trigger an alert in Sentry")
    return True

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

structlog-sentry-lambda-1.4.2.tar.gz (14.2 kB view hashes)

Uploaded Source

Built Distribution

structlog_sentry_lambda-1.4.2-py3-none-any.whl (13.7 kB view hashes)

Uploaded Python 3

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