-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable log/slog from Go v1.21 and migrate scheduler. (#845)
* Enable slogging and add a way to set slog.Attrs in the context. Signed-off-by: Caleb Brown <[email protected]> * Migrate scheduler of zap logging to slog. Signed-off-by: Caleb Brown <[email protected]> * Bump golang version for the Dockerfiles. Signed-off-by: Caleb Brown <[email protected]> * Bump go version for workflows. Signed-off-by: Caleb Brown <[email protected]> * Add tags to the dockerfile Signed-off-by: Caleb Brown <[email protected]> --------- Signed-off-by: Caleb Brown <[email protected]>
- Loading branch information
1 parent
039fb69
commit 94465a1
Showing
13 changed files
with
196 additions
and
28 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
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
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
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
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,46 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
) | ||
|
||
var contextAttrSliceKey = struct{}{} | ||
|
||
func attrSliceFromContext(ctx context.Context) []slog.Attr { | ||
if v := ctx.Value(contextAttrSliceKey); v != nil { | ||
return v.([]slog.Attr) | ||
} | ||
return nil | ||
} | ||
|
||
// ContextWithAttrs is used to add attrs to the context so they are included | ||
// when logs are output. | ||
func ContextWithAttrs(ctx context.Context, attr ...slog.Attr) context.Context { | ||
if len(attr) == 0 { | ||
return ctx | ||
} | ||
attrSlice := attrSliceFromContext(ctx) | ||
attrSlice = append(attrSlice, attr...) | ||
return context.WithValue(ctx, contextAttrSliceKey, attrSlice) | ||
} | ||
|
||
type contextLogHandler struct { | ||
slog.Handler | ||
} | ||
|
||
func (h *contextLogHandler) Handle(ctx context.Context, r slog.Record) error { | ||
attrSlice := attrSliceFromContext(ctx) | ||
if attrSlice != nil { | ||
r.AddAttrs(attrSlice...) | ||
} | ||
return h.Handler.Handle(ctx, r) | ||
} | ||
|
||
// NewContextLogHandler returns a new slog.Handler that will pass the attrs set | ||
// using ContextWithAttrs to handler when Handle is called. | ||
func NewContextLogHandler(handler slog.Handler) slog.Handler { | ||
return &contextLogHandler{ | ||
Handler: handler, | ||
} | ||
} |
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,108 @@ | ||
package log_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"log/slog" | ||
|
||
"github.com/ossf/package-analysis/internal/log" | ||
) | ||
|
||
type testHandler struct { | ||
slog.Handler | ||
|
||
r slog.Record | ||
} | ||
|
||
func (h *testHandler) Enabled(_ context.Context, _ slog.Level) bool { | ||
return true | ||
} | ||
|
||
func (h *testHandler) Handle(ctx context.Context, r slog.Record) error { | ||
h.r = r | ||
return nil | ||
} | ||
|
||
func assertRecordAttrs(t *testing.T, r slog.Record, attrs []slog.Attr) { | ||
t.Helper() | ||
|
||
wantLen := len(attrs) | ||
gotLen := r.NumAttrs() | ||
if wantLen != gotLen { | ||
t.Errorf("record.NumAttrs() = %v; want %v", gotLen, wantLen) | ||
} | ||
|
||
r.Attrs(func(a slog.Attr) bool { | ||
for _, attr := range attrs { | ||
if a.Equal(attr) { | ||
return true | ||
} | ||
} | ||
t.Errorf("unexpected attr %v", a) | ||
return true | ||
}) | ||
} | ||
|
||
func TestContextWithAttrs(t *testing.T) { | ||
attr1 := slog.Any("hello", "world") | ||
attr2 := slog.Int("meaning", 42) | ||
attr3 := slog.String("a", "b") | ||
|
||
h := &testHandler{} | ||
logger := slog.New(log.NewContextLogHandler(h)) | ||
|
||
ctx := context.Background() | ||
|
||
// Add attrs to the context and ensure they are used. | ||
ctx = log.ContextWithAttrs(ctx, attr1, attr2) | ||
logger.InfoContext(ctx, "test", "a", "b") | ||
assertRecordAttrs(t, h.r, []slog.Attr{attr1, attr2, attr3}) | ||
} | ||
|
||
func TestContextWithAttrs_InnerCtx(t *testing.T) { | ||
attr1 := slog.Any("hello", "world") | ||
attr2 := slog.Int("meaning", 42) | ||
attr3 := slog.Any("complex", struct{ a string }{a: "string"}) | ||
|
||
h := &testHandler{} | ||
logger := slog.New(log.NewContextLogHandler(h)) | ||
|
||
ctx := context.Background() | ||
ctx = log.ContextWithAttrs(ctx, attr1, attr2) | ||
|
||
// Add more attrs to the context and ensure they are used. | ||
innerCtx := log.ContextWithAttrs(ctx, attr3) | ||
logger.InfoContext(innerCtx, "test") | ||
assertRecordAttrs(t, h.r, []slog.Attr{attr1, attr2, attr3}) | ||
} | ||
|
||
func TestContextWithAttrs_OuterAfterInnerCtx(t *testing.T) { | ||
attr1 := slog.Any("hello", "world") | ||
attr2 := slog.Int("meaning", 42) | ||
attr3 := slog.Any("complex", struct{ a string }{a: "string"}) | ||
|
||
h := &testHandler{} | ||
logger := slog.New(log.NewContextLogHandler(h)) | ||
|
||
ctx := context.Background() | ||
ctx = log.ContextWithAttrs(ctx, attr1, attr2) | ||
_ = log.ContextWithAttrs(ctx, attr3) | ||
|
||
// Use the earlier context to ensure the innerCtx attrs are not included. | ||
logger.InfoContext(ctx, "test") | ||
assertRecordAttrs(t, h.r, []slog.Attr{attr1, attr2}) | ||
} | ||
|
||
func TestContextWithAttrs_NoAttrs(t *testing.T) { | ||
attr1 := slog.String("a", "b") | ||
|
||
h := &testHandler{} | ||
logger := slog.New(log.NewContextLogHandler(h)) | ||
|
||
ctx := context.Background() | ||
ctx = log.ContextWithAttrs(ctx) | ||
|
||
logger.InfoContext(ctx, "test", "a", "b") | ||
assertRecordAttrs(t, h.r, []slog.Attr{attr1}) | ||
} |
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