Skip to content

Commit

Permalink
refactor: fixed inefficient []zapcore.Field insertion
Browse files Browse the repository at this point in the history
Signed-off-by: junya koyama <arukiidou@yahoo.co.jp>
  • Loading branch information
arukiidou committed Feb 3, 2024
1 parent f09e134 commit 104e04f
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions exp/zapslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"context"
"log/slog"
"runtime"
"slices"

"go.uber.org/zap"
"go.uber.org/zap/internal/stacktrace"
Expand Down Expand Up @@ -167,7 +166,7 @@ func (h *Handler) Handle(ctx context.Context, record slog.Record) error {
record.Attrs(func(attr slog.Attr) bool {
f := convertAttrToField(attr)
if h.holdGroup != "" && f != zap.Skip() {
fields = slices.Insert(fields, 0, zap.Namespace(h.holdGroup))
fields = append(fields, zap.Namespace(h.holdGroup))
h.holdGroup = ""
}
fields = append(fields, f)
Expand All @@ -181,11 +180,11 @@ func (h *Handler) Handle(ctx context.Context, record slog.Record) error {
// WithAttrs returns a new Handler whose attributes consist of
// both the receiver's attributes and the arguments.
func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler {
fields := make([]zapcore.Field, 0, len(attrs))
fields := make([]zapcore.Field, 0, len(attrs)+1)
for _, attr := range attrs {
f := convertAttrToField(attr)
if h.holdGroup != "" && f != zap.Skip() {
fields = slices.Insert(fields, 0, zap.Namespace(h.holdGroup))
fields = append(fields, zap.Namespace(h.holdGroup))
h.holdGroup = ""
}
fields = append(fields, f)
Expand Down

0 comments on commit 104e04f

Please sign in to comment.