Skip to content

Commit

Permalink
Revert "reduce append cycles"
Browse files Browse the repository at this point in the history
This reverts commit 6ee6667.
  • Loading branch information
phuslu committed May 6, 2024
1 parent 6ee6667 commit f076451
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,47 +129,41 @@ func (h *slogJSONHandler) handle(_ context.Context, r slog.Record) error {
e := epool.Get().(*Entry)
e.buf = e.buf[:0]

e.buf = append(e.buf, '{')

// time
if !r.Time.IsZero() {
e.buf = append(e.buf, (`{"` + slog.TimeKey + `":"`)...)
e.buf = append(e.buf, '"')
e.buf = append(e.buf, slog.TimeKey...)
e.buf = append(e.buf, `":"`...)
e.buf = r.Time.AppendFormat(e.buf, time.RFC3339Nano)
// level
switch r.Level {
case slog.LevelDebug:
e.buf = append(e.buf, (`","` + slog.LevelKey + `":"DEBUG"`)...)
case slog.LevelInfo:
e.buf = append(e.buf, (`","` + slog.LevelKey + `":"INFO"`)...)
case slog.LevelWarn:
e.buf = append(e.buf, (`","` + slog.LevelKey + `":"WARN"`)...)
case slog.LevelError:
e.buf = append(e.buf, (`","` + slog.LevelKey + `":"ERROR"`)...)
default:
e.buf = append(e.buf, (`","` + slog.LevelKey + `":"`)...)
e.buf = append(e.buf, r.Level.String()...)
e.buf = append(e.buf, '"')
}
} else {
// level
switch r.Level {
case slog.LevelDebug:
e.buf = append(e.buf, (`{"` + slog.LevelKey + `":"DEBUG"`)...)
case slog.LevelInfo:
e.buf = append(e.buf, (`{"` + slog.LevelKey + `":"INFO"`)...)
case slog.LevelWarn:
e.buf = append(e.buf, (`{"` + slog.LevelKey + `":"WARN"`)...)
case slog.LevelError:
e.buf = append(e.buf, (`{"` + slog.LevelKey + `":"ERROR"`)...)
default:
e.buf = append(e.buf, (`{"` + slog.LevelKey + `":"`)...)
e.buf = append(e.buf, r.Level.String()...)
e.buf = append(e.buf, '"')
}
e.buf = append(e.buf, `",`...)
}

// level
e.buf = append(e.buf, '"')
e.buf = append(e.buf, slog.LevelKey...)
switch r.Level {
case slog.LevelDebug:
e.buf = append(e.buf, `":"DEBUG"`...)
case slog.LevelInfo:
e.buf = append(e.buf, `":"INFO"`...)
case slog.LevelWarn:
e.buf = append(e.buf, `":"WARN"`...)
case slog.LevelError:
e.buf = append(e.buf, `":"ERROR"`...)
default:
e.buf = append(e.buf, `":"`...)
e.buf = append(e.buf, r.Level.String()...)
e.buf = append(e.buf, '"')
}

// source
if h.options.AddSource && r.PC != 0 {
name, file, line := pcNameFileLine(r.PC)
e.buf = append(e.buf, (`,"` + slog.SourceKey + `":{"function":"`)...)
e.buf = append(e.buf, ',', '"')
e.buf = append(e.buf, slog.SourceKey...)
e.buf = append(e.buf, `":{"function":"`...)
e.buf = append(e.buf, name...)
e.buf = append(e.buf, `","file":"`...)
e.buf = append(e.buf, file...)
Expand All @@ -179,9 +173,7 @@ func (h *slogJSONHandler) handle(_ context.Context, r slog.Record) error {
}

// msg
e.buf = append(e.buf, (`,"` + slog.MessageKey + `":"`)...)
e.string(r.Message)
e.buf = append(e.buf, '"')
e = e.Str(slog.MessageKey, r.Message)

// with
if b := h.entry.buf; len(b) != 0 {
Expand Down

0 comments on commit f076451

Please sign in to comment.