Skip to content

Commit d6ee397

Browse files
ulya-sidorinaasmyasnikov
authored andcommitted
feat(logger): add WithFields/FieldsFromContext methods for context (#1869)
Co-authored-by: Aleksey Myasnikov <asmyasnikov@ydb.tech>
1 parent df48e91 commit d6ee397

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
* Added helper methods `log.WithFields` and `log.FieldsFromContext` for working with structured logging fields via context.
2+
These methods allow adding custom fields to the context, which are later extracted by the logger.
3+
14
## v3.116.4
2-
* Fixed error handling in internaltopicreader.addOnTransactionCompletedHandler
5+
* Fixed error handling in `internaltopicreader.addOnTransactionCompletedHandler`
36

47
## v3.116.3
58
* Default grpc message size adjusted to the server's defaults

log/context.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
)
66

77
type (
8-
ctxLevelKey struct{}
9-
ctxNamesKey struct{}
8+
ctxLevelKey struct{}
9+
ctxNamesKey struct{}
10+
ctxFieldsKey struct{}
1011
)
1112

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

40+
func WithFields(ctx context.Context, fields ...Field) context.Context {
41+
return context.WithValue(ctx, ctxFieldsKey{}, append(
42+
FieldsFromContext(ctx),
43+
fields...,
44+
))
45+
}
46+
47+
func FieldsFromContext(ctx context.Context) []Field {
48+
if fields, has := ctx.Value(ctxFieldsKey{}).([]Field); has && len(fields) > 0 {
49+
return fields
50+
}
51+
52+
return nil
53+
}
54+
3955
func with(ctx context.Context, lvl Level, names ...string) context.Context {
4056
return WithLevel(WithNames(ctx, names...), lvl)
4157
}

log/context_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,37 @@ func TestWithNamesRaceRegression(t *testing.T) {
9090
}
9191
})
9292
}
93+
94+
func TestFieldsFromContext(t *testing.T) {
95+
for _, tt := range []struct {
96+
ctx context.Context //nolint:containedctx
97+
fields []Field
98+
}{
99+
{
100+
ctx: context.Background(),
101+
fields: nil,
102+
},
103+
{
104+
ctx: WithFields(context.Background(), String("a", "1"), String("b", "1")),
105+
fields: []Field{String("a", "1"), String("b", "1")},
106+
},
107+
{
108+
ctx: WithFields(
109+
WithFields(context.Background(), String("a", "1"), String("b", "1")),
110+
String("a", "1"), String("b", "1"),
111+
),
112+
fields: []Field{String("a", "1"), String("b", "1"), String("a", "1"), String("b", "1")},
113+
},
114+
{
115+
ctx: WithFields(
116+
WithFields(context.Background(), String("a", "1"), String("b", "1")),
117+
String("a", "3"),
118+
),
119+
fields: []Field{String("a", "1"), String("b", "1"), String("a", "3")},
120+
},
121+
} {
122+
t.Run("", func(t *testing.T) {
123+
require.Equal(t, tt.fields, FieldsFromContext(tt.ctx))
124+
})
125+
}
126+
}

log/logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (l *defaultLogger) Log(ctx context.Context, msg string, fields ...Field) {
9292

9393
_, _ = io.WriteString(l.w, l.format(
9494
NamesFromContext(ctx),
95-
l.appendFields(msg, fields...),
95+
appendFieldsToMsg(msg, append(FieldsFromContext(ctx), fields...)...),
9696
lvl,
9797
)+"\n")
9898
}
@@ -115,7 +115,7 @@ func wrapLogger(l Logger, opts ...Option) *wrapper {
115115
return ll
116116
}
117117

118-
func (l *defaultLogger) appendFields(msg string, fields ...Field) string {
118+
func appendFieldsToMsg(msg string, fields ...Field) string {
119119
if len(fields) == 0 {
120120
return msg
121121
}

0 commit comments

Comments
 (0)