Skip to content
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
* Added helper methods `log.WithFields` and `log.FieldsFromContext` for working with structured logging fields via context.
These methods allow adding custom fields to the context, which are later extracted by the logger.

## v3.116.4
* Fixed error handling in internaltopicreader.addOnTransactionCompletedHandler
* Fixed error handling in `internaltopicreader.addOnTransactionCompletedHandler`

## v3.116.3
* Default grpc message size adjusted to the server's defaults
Expand Down
20 changes: 18 additions & 2 deletions log/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

type (
ctxLevelKey struct{}
ctxNamesKey struct{}
ctxLevelKey struct{}
ctxNamesKey struct{}
ctxFieldsKey struct{}
)

func WithLevel(ctx context.Context, lvl Level) context.Context {
Expand Down Expand Up @@ -36,6 +37,21 @@ func NamesFromContext(ctx context.Context) []string {
return v[:len(v):len(v)] // prevent re
}

func WithFields(ctx context.Context, fields ...Field) context.Context {
return context.WithValue(ctx, ctxFieldsKey{}, append(
FieldsFromContext(ctx),
fields...,
))
}

func FieldsFromContext(ctx context.Context) []Field {
if fields, has := ctx.Value(ctxFieldsKey{}).([]Field); has && len(fields) > 0 {
return fields
}

return nil
}

func with(ctx context.Context, lvl Level, names ...string) context.Context {
return WithLevel(WithNames(ctx, names...), lvl)
}
34 changes: 34 additions & 0 deletions log/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,37 @@ func TestWithNamesRaceRegression(t *testing.T) {
}
})
}

func TestFieldsFromContext(t *testing.T) {
for _, tt := range []struct {
ctx context.Context //nolint:containedctx
fields []Field
}{
{
ctx: context.Background(),
fields: nil,
},
{
ctx: WithFields(context.Background(), String("a", "1"), String("b", "1")),
fields: []Field{String("a", "1"), String("b", "1")},
},
{
ctx: WithFields(
WithFields(context.Background(), String("a", "1"), String("b", "1")),
String("a", "1"), String("b", "1"),
),
fields: []Field{String("a", "1"), String("b", "1"), String("a", "1"), String("b", "1")},
},
{
ctx: WithFields(
WithFields(context.Background(), String("a", "1"), String("b", "1")),
String("a", "3"),
),
fields: []Field{String("a", "1"), String("b", "1"), String("a", "3")},
},
} {
t.Run("", func(t *testing.T) {
require.Equal(t, tt.fields, FieldsFromContext(tt.ctx))
})
}
}
4 changes: 2 additions & 2 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (l *defaultLogger) Log(ctx context.Context, msg string, fields ...Field) {

_, _ = io.WriteString(l.w, l.format(
NamesFromContext(ctx),
l.appendFields(msg, fields...),
appendFieldsToMsg(msg, append(FieldsFromContext(ctx), fields...)...),
lvl,
)+"\n")
}
Expand All @@ -115,7 +115,7 @@ func wrapLogger(l Logger, opts ...Option) *wrapper {
return ll
}

func (l *defaultLogger) appendFields(msg string, fields ...Field) string {
func appendFieldsToMsg(msg string, fields ...Field) string {
if len(fields) == 0 {
return msg
}
Expand Down
Loading