-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
58 lines (44 loc) · 1.19 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package slogawslambda
import (
"context"
"log/slog"
"os"
"github.com/aws/aws-lambda-go/lambdacontext"
)
type LambdaHandler struct {
slog.JSONHandler
}
func NewAWSLambdaHandler(ctx context.Context, opts *slog.HandlerOptions) slog.Handler {
if opts == nil {
opts = &slog.HandlerOptions{}
}
// Set the level based on the environment variable `LOG_LEVEL`
if opts.Level == nil {
opts.Level = getLogLevel()
}
// Retrieve AWS Request ID and lambda function arn
lc, found := lambdacontext.FromContext(ctx)
if !found {
return slog.NewJSONHandler(os.Stdout, opts)
}
requestID := lc.AwsRequestID
arn := lc.InvokedFunctionArn
// Create the Handler using the attributes from lambda context
return slog.NewJSONHandler(os.Stdout, opts).
WithAttrs([]slog.Attr{slog.String("function_arn", arn)}).
WithAttrs([]slog.Attr{slog.String("request_id", requestID)})
}
func getLogLevel() slog.Leveler {
str, found := os.LookupEnv("LOG_LEVEL")
// If no value is set, use Info as default Level
if !found {
return slog.LevelInfo
}
var l slog.Level
err := l.UnmarshalText([]byte(str))
// If invalid value is set, use Info as default Level
if err != nil {
return slog.LevelInfo
}
return l
}