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)
| 52 | // By default, only requestId is injected. Use WithFunctionARN or WithTenantID to include more. |
| 53 | // See the package examples for usage. |
| 54 | func 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...)). |
searching dependent graphs…