MCPcopy
hub / github.com/aws/aws-lambda-go / NewLogHandler

Function NewLogHandler

lambdacontext/logger.go:54–74  ·  view source on GitHub ↗

NewLogHandler returns a [slog.Handler] for AWS Lambda structured logging. It reads AWS_LAMBDA_LOG_FORMAT and AWS_LAMBDA_LOG_LEVEL from environment, and injects requestId from Lambda context into each log record. By default, only requestId is injected. Use WithFunctionARN or WithTenantID to include

(opts ...LogOption)

Source from the content-addressed store, hash-verified

52// By default, only requestId is injected. Use WithFunctionARN or WithTenantID to include more.
53// See the package examples for usage.
54func NewLogHandler(opts ...LogOption) slog.Handler {
55 options := &logOptions{}
56 for _, opt := range opts {
57 opt(options)
58 }
59
60 level := parseLogLevel()
61 handlerOpts := &slog.HandlerOptions{
62 Level: level,
63 ReplaceAttr: ReplaceAttr,
64 }
65
66 var h slog.Handler
67 if logFormat == "JSON" {
68 h = slog.NewJSONHandler(os.Stdout, handlerOpts)
69 } else {
70 h = slog.NewTextHandler(os.Stdout, handlerOpts)
71 }
72
73 return &lambdaHandler{handler: h, fields: options.fields}
74}
75
76// NewLogger returns a [*slog.Logger] configured for AWS Lambda structured logging.
77// This is a convenience function equivalent to slog.New(NewLogHandler(opts...)).

Callers 4

ExampleNewLogHandlerFunction · 0.92
TestNewLogHandlerFunction · 0.85
NewLoggerFunction · 0.85

Calls 1

parseLogLevelFunction · 0.85

Tested by 3

ExampleNewLogHandlerFunction · 0.74
TestNewLogHandlerFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…