-
Notifications
You must be signed in to change notification settings - Fork 575
Adding structured logging helper #614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
lambdacontext/logger.go
Outdated
| } | ||
|
|
||
| // Attrs returns Lambda context fields as slog-compatible key-value pairs. | ||
| // For most use cases, using [Handler] with slog.InfoContext is preferred. |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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())) |
There was a problem hiding this comment.
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.
| slog.SetDefault(slog.New(lambdacontext.LogHandler( | ||
| lambdacontext.WithFields(lambdacontext.FieldFunctionARN(), lambdacontext.FieldTenantID()), | ||
| ))) |
There was a problem hiding this comment.
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 } }
}
}
Issue #, if available:
Description of changes:
Adding helper for structured logging so users can log as JSON with slog easier than before.
LogFormatNameandLogLevelNameare added tocontext.goas well so it can be easier and cleaner to setup a structured logger as well.Tests:
Added tests for
logger.goBy submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.