-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
) | ||
|
||
type ctxKeySLOGAttr string | ||
|
||
var ctxKeySlOGAttr = ctxKeySLOGAttr("attr") | ||
|
||
// AddSLOGAttrToCTX adds slog attr to ctx | ||
func AddSLOGAttrToCTX(ctx context.Context, attrs []slog.Attr) context.Context { | ||
oldAttrs := GetSLOGAttrFromCTX(ctx) | ||
if oldAttrs != nil { | ||
attrs = append(oldAttrs, attrs...) | ||
} | ||
return context.WithValue(ctx, ctxKeySlOGAttr, attrs) | ||
} | ||
|
||
// GetSLOGAttrFromCTX returns slog attrs from ctx | ||
func GetSLOGAttrFromCTX(ctx context.Context) []slog.Attr { | ||
attrs, ok := ctx.Value(ctxKeySlOGAttr).([]slog.Attr) | ||
if !ok { | ||
attrs = nil | ||
} | ||
return attrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
) | ||
|
||
type CTXAttrHandler struct { | ||
slog.Handler | ||
} | ||
|
||
// NewCTXAttrHandler | ||
// | ||
// is a slog.Handler that adds slog.Attr to slog.Record from context.Context | ||
func NewCTXAttrHandler(serviceName string, mainHandler slog.Handler) CTXAttrHandler { | ||
mainHandler.WithAttrs([]slog.Attr{ | ||
slog.String("service", serviceName), | ||
}, | ||
) | ||
|
||
return CTXAttrHandler{ | ||
Handler: mainHandler, | ||
} | ||
} | ||
|
||
func (p CTXAttrHandler) Handle(ctx context.Context, record slog.Record) error { | ||
attrs := GetSLOGAttrFromCTX(ctx) | ||
if attrs == nil { | ||
return p.Handler.Handle(ctx, record) | ||
} | ||
|
||
record.AddAttrs(attrs...) | ||
|
||
return p.Handler.Handle(ctx, record) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package log | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"log/slog" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestCTXAttrHandler(t *testing.T) { | ||
type kv struct { | ||
key string | ||
value string | ||
} | ||
|
||
type args struct { | ||
kv []kv | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "simple test with one attr in ctx", | ||
args: args{ | ||
kv: []kv{ | ||
{ | ||
key: "req-id", | ||
value: "123", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "simple test with two attr in ctx", | ||
args: args{ | ||
kv: []kv{ | ||
{ | ||
key: "req-id", | ||
value: "123", | ||
}, | ||
{ | ||
key: "name", | ||
value: "John Doe", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := context.Background() | ||
for _, kv := range tt.args.kv { | ||
ctx = AddSLOGAttrToCTX(ctx, []slog.Attr{slog.String(kv.key, kv.value)}) | ||
} | ||
|
||
buf := &bytes.Buffer{} | ||
defer buf.Truncate(len(buf.Bytes())) | ||
|
||
handler := NewCTXAttrHandler("test", slog.NewJSONHandler(buf, nil)) | ||
logger := slog.New(handler) | ||
logger.InfoContext(ctx, "test") | ||
str := buf.String() | ||
|
||
for _, kv := range tt.args.kv { | ||
if !strings.Contains(str, kv.key) { | ||
t.Errorf("JSONCTXHandlerBase() = %v, want %v", str, kv.key) | ||
} | ||
if !strings.Contains(str, kv.value) { | ||
t.Errorf("JSONCTXHandlerBase() = %v, want %v", str, kv.value) | ||
} | ||
} | ||
}) | ||
} | ||
} |