Skip to content

Conversation

@anzheyazzz
Copy link

Issue #, if available:

Description of changes:

Adding helper for structured logging so users can log as JSON with slog easier than before.
LogFormatName and LogLevelName are added to context.go as well so it can be easier and cleaner to setup a structured logger as well.

Tests:

Added tests for logger.go

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov-commenter
Copy link

codecov-commenter commented Jan 14, 2026

Codecov Report

❌ Patch coverage is 77.77778% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.21%. Comparing base (6252f73) to head (31a7a20).

Files with missing lines Patch % Lines
lambdacontext/logger.go 77.04% 14 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #614      +/-   ##
==========================================
+ Coverage   73.67%   74.21%   +0.54%     
==========================================
  Files          35       36       +1     
  Lines        1341     1404      +63     
==========================================
+ Hits          988     1042      +54     
- Misses        277      286       +9     
  Partials       76       76              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

}

// Attrs returns Lambda context fields as slog-compatible key-value pairs.
// For most use cases, using [Handler] with slog.InfoContext is preferred.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL slog.InfoContext/WarnContext/etc

- Rename Handler() to LogHandler()
- Rename LogFormatName/LogLevelName to LogFormat/LogLevel
- Use functional options pattern with WithFields()
- Use Field functions (FieldFunctionARN, FieldTenantID) for immutability
- Add example tests in separate file with go1.21 build tag
//
// By default, only requestId is injected. Use WithFields to include more.
// See the package examples for usage.
func LogHandler(opts ...LogOption) slog.Handler {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewLogHandler would be consistent with other constructors

// The handler automatically injects requestId from Lambda context into each log record.
func ExampleLogHandler() {
// Set up the Lambda-aware slog handler
slog.SetDefault(slog.New(lambdacontext.LogHandler()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking to simplify this to slog.SetDefault(lambdacontext.NewLogger()) where NewLogger returns a *slog.Logger.

Comment on lines +31 to +33
slog.SetDefault(slog.New(lambdacontext.LogHandler(
lambdacontext.WithFields(lambdacontext.FieldFunctionARN(), lambdacontext.FieldTenantID()),
)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd prefer just option functions that setup the fields, rather than a fields option. To reduce the line noise and nesting.

lambdacontext.LogHandler(
    lambdacontext.WithFunctionARN(), 
    lambdacontext.WithTenantID(),
)
func WithFunctionARN() LoggerOption {
    return func(*loggerOptions) {
        o.fields = append(o.fields, field{"functionArn", func(lc *LambdaContext) string { return lc.InvokedFunctionArn } }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants