https://github.com/unfunco/powerslog
A slog handler that enriches structured logs with key fields captured from an AWS Lambda context.
https://github.com/unfunco/powerslog
amazon-web-services aws aws-lambda aws-lambda-golang go golang lambda lambda-context powertools slog slog-handler structured-logging
Last synced: 5 months ago
JSON representation
A slog handler that enriches structured logs with key fields captured from an AWS Lambda context.
- Host: GitHub
- URL: https://github.com/unfunco/powerslog
- Owner: unfunco
- License: mit
- Created: 2024-04-01T09:58:08.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-30T16:06:02.000Z (about 2 years ago)
- Last Synced: 2025-06-09T14:49:33.735Z (about 1 year ago)
- Topics: amazon-web-services, aws, aws-lambda, aws-lambda-golang, go, golang, lambda, lambda-context, powertools, slog, slog-handler, structured-logging
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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
```go
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
```bash
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].
[daniel morris]: https://unfun.co
[go]: https://go.dev
[mit license]: LICENSE.md