-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
7 changed files
with
219 additions
and
94 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,23 @@ | ||
package applog | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"github.com/hareku/fanbox-dl/internal/ctxval" | ||
) | ||
|
||
type ContextValueLogHandler struct { | ||
slog.Handler | ||
} | ||
|
||
func NewContextValueLogHandler(h slog.Handler) *ContextValueLogHandler { | ||
return &ContextValueLogHandler{Handler: h} | ||
} | ||
|
||
func (h *ContextValueLogHandler) Handle(ctx context.Context, r slog.Record) error { | ||
if attrs, ok := ctxval.GetSlogAttrs(ctx); ok { | ||
r.AddAttrs(attrs...) | ||
} | ||
return h.Handler.Handle(ctx, r) | ||
} |
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,22 @@ | ||
package applog | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
func InitLogger(verbose bool) { | ||
level := slog.LevelInfo | ||
if verbose { | ||
level = slog.LevelDebug | ||
} | ||
|
||
var h slog.Handler | ||
h = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ | ||
Level: level, | ||
}) | ||
h = NewContextValueLogHandler(h) | ||
|
||
logger := slog.New(h) | ||
slog.SetDefault(logger) | ||
} |
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,23 @@ | ||
package ctxval | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
) | ||
|
||
type slogAttrsKey struct{} | ||
|
||
func AddSlogAttrs(ctx context.Context, attrs ...slog.Attr) context.Context { | ||
s, ok := GetSlogAttrs(ctx) | ||
if !ok { | ||
s = []slog.Attr{} | ||
} | ||
|
||
s = append(s, attrs...) | ||
return context.WithValue(ctx, slogAttrsKey{}, s) | ||
} | ||
|
||
func GetSlogAttrs(ctx context.Context) ([]slog.Attr, bool) { | ||
v, ok := ctx.Value(slogAttrsKey{}).([]slog.Attr) | ||
return v, ok | ||
} |
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,31 @@ | ||
package ctxval | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAddSlogAttrs(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
_, ok := GetSlogAttrs(ctx) | ||
require.False(t, ok) | ||
|
||
ctx = AddSlogAttrs(ctx, slog.String("k1", "v1")) | ||
|
||
v, ok := GetSlogAttrs(ctx) | ||
require.True(t, ok) | ||
require.Equal(t, []slog.Attr{slog.String("k1", "v1")}, v) | ||
|
||
ctx2 := AddSlogAttrs(ctx, slog.String("k2", "v2")) | ||
v, ok = GetSlogAttrs(ctx2) | ||
require.True(t, ok) | ||
require.Equal(t, []slog.Attr{slog.String("k1", "v1"), slog.String("k2", "v2")}, v) | ||
|
||
v, ok = GetSlogAttrs(ctx) | ||
require.True(t, ok) | ||
require.Equal(t, []slog.Attr{slog.String("k1", "v1")}, v, "original context should not be modified") | ||
} |
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