Coder Social home page Coder Social logo

unfunco / powerslog Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 25 KB

A slog handler that enriches structured logs with key fields captured from an AWS Lambda context.

License: MIT License

Go 100.00%
amazon-web-services aws aws-lambda aws-lambda-golang go golang lambda powertools slog slog-handler

powerslog's Introduction

Powerslog

A slog handler that captures key fields from an AWS Lambda context and produces structured logs with the same fields as the Powertools loggers for Python and TypeScript.

Getting started

Requirements

  • Go 1.22+

Installation and usage

package main

import (
    "context"
    "log/slog"
    "net/http"
    "os"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/unfunco/powerslog"
)

func handler(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    logger := ctx.Value("logger").(*slog.Logger)
    logger.Info("Request received", slog.Any("event", event))

    return events.APIGatewayProxyResponse{
        StatusCode: http.StatusNoContent,
    }, nil
}

func main() {
    jsonHandler := slog.NewJSONHandler(os.Stdout, nil)
    powerslogHandler := powerslog.NewHandler(jsonHandler)
    logger := slog.New(powerslogHandler)

    ctx := context.WithValue(context.Background(), "logger", logger)
    lambda.StartWithOptions(handler, lambda.WithContext(ctx))
}

Development and testing

cd lambda
GOOS=linux GOARCH=arm64 go build -tags lambda.norpc -o bootstrap main.go
sam deploy --guided

License

© 2024 Daniel Morris
Made available under the terms of the MIT License.

powerslog's People

Contributors

github-actions[bot] avatar unfunco avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.