Skip to content

Commit

Permalink
增加 ReplaceAttr 的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Dec 18, 2023
1 parent b872519 commit aaa3c40
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 51 deletions.
14 changes: 7 additions & 7 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ goos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor

BenchmarkLogitLogger-2 1465222 815 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerTextHandler-2 1000000 1097 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerJsonHandler-2 803572 1414 ns/op 120 B/op 3 allocs/op
BenchmarkLogitLoggerPrint-2 1213560 999 ns/op 48 B/op 1 allocs/op
BenchmarkLogitLogger-2 1486184 810 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerTextHandler-2 1000000 1080 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerJsonHandler-2 847864 1393 ns/op 120 B/op 3 allocs/op
BenchmarkLogitLoggerPrint-2 1222302 981 ns/op 48 B/op 1 allocs/op
BenchmarkSlogLoggerTextHandler-2 725522 1629 ns/op 0 B/op 0 allocs/op
BenchmarkSlogLoggerJsonHandler-2 583214 2030 ns/op 120 B/op 3 allocs/op
BenchmarkZeroLogLogger-2 1929276 613 ns/op 0 B/op 0 allocs/op
BenchmarkZapLogger-2 976855 1168 ns/op 216 B/op 2 allocs/op
BenchmarkLogrusLogger-2 231723 4927 ns/op 2080 B/op 32 allocs/op

BenchmarkLogitFile-2 580012 2011 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBuffer-2 1391960 865 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBatch-2 1331029 894 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFile-2 624774 1935 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBuffer-2 1378076 873 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBatch-2 1367479 883 ns/op 0 B/op 0 allocs/op
BenchmarkSlogFile-2 407590 2944 ns/op 0 B/op 0 allocs/op
BenchmarkZeroLogFile-2 634375 1810 ns/op 0 B/op 0 allocs/op
BenchmarkZapFile-2 382790 2641 ns/op 216 B/op 2 allocs/op
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,19 @@ goos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor

BenchmarkLogitLogger-2 1465222 815 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerTextHandler-2 1000000 1097 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerJsonHandler-2 803572 1414 ns/op 120 B/op 3 allocs/op
BenchmarkLogitLoggerPrint-2 1213560 999 ns/op 48 B/op 1 allocs/op
BenchmarkLogitLogger-2 1486184 810 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerTextHandler-2 1000000 1080 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerJsonHandler-2 847864 1393 ns/op 120 B/op 3 allocs/op
BenchmarkLogitLoggerPrint-2 1222302 981 ns/op 48 B/op 1 allocs/op
BenchmarkSlogLoggerTextHandler-2 725522 1629 ns/op 0 B/op 0 allocs/op
BenchmarkSlogLoggerJsonHandler-2 583214 2030 ns/op 120 B/op 3 allocs/op
BenchmarkZeroLogLogger-2 1929276 613 ns/op 0 B/op 0 allocs/op
BenchmarkZapLogger-2 976855 1168 ns/op 216 B/op 2 allocs/op
BenchmarkLogrusLogger-2 231723 4927 ns/op 2080 B/op 32 allocs/op

BenchmarkLogitFile-2 580012 2011 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBuffer-2 1391960 865 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBatch-2 1331029 894 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFile-2 624774 1935 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBuffer-2 1378076 873 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBatch-2 1367479 883 ns/op 0 B/op 0 allocs/op
BenchmarkSlogFile-2 407590 2944 ns/op 0 B/op 0 allocs/op
BenchmarkZeroLogFile-2 634375 1810 ns/op 0 B/op 0 allocs/op
BenchmarkZapFile-2 382790 2641 ns/op 216 B/op 2 allocs/op
Expand Down
14 changes: 7 additions & 7 deletions _examples/performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ goos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor
BenchmarkLogitLogger-2 1465222 815 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerTextHandler-2 1000000 1097 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerJsonHandler-2 803572 1414 ns/op 120 B/op 3 allocs/op
BenchmarkLogitLoggerPrint-2 1213560 999 ns/op 48 B/op 1 allocs/op
BenchmarkLogitLogger-2 1486184 810 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerTextHandler-2 1000000 1080 ns/op 0 B/op 0 allocs/op
BenchmarkLogitLoggerJsonHandler-2 847864 1393 ns/op 120 B/op 3 allocs/op
BenchmarkLogitLoggerPrint-2 1222302 981 ns/op 48 B/op 1 allocs/op
BenchmarkSlogLoggerTextHandler-2 725522 1629 ns/op 0 B/op 0 allocs/op
BenchmarkSlogLoggerJsonHandler-2 583214 2030 ns/op 120 B/op 3 allocs/op
BenchmarkZeroLogLogger-2 1929276 613 ns/op 0 B/op 0 allocs/op
BenchmarkZapLogger-2 976855 1168 ns/op 216 B/op 2 allocs/op
BenchmarkLogrusLogger-2 231723 4927 ns/op 2080 B/op 32 allocs/op
BenchmarkLogitFile-2 580012 2011 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBuffer-2 1391960 865 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBatch-2 1331029 894 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFile-2 624774 1935 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBuffer-2 1378076 873 ns/op 0 B/op 0 allocs/op
BenchmarkLogitFileWithBatch-2 1367479 883 ns/op 0 B/op 0 allocs/op
BenchmarkSlogFile-2 407590 2944 ns/op 0 B/op 0 allocs/op
BenchmarkZeroLogFile-2 634375 1810 ns/op 0 B/op 0 allocs/op
BenchmarkZapFile-2 382790 2641 ns/op 216 B/op 2 allocs/op
Expand Down
82 changes: 53 additions & 29 deletions core/handler/mix.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ type mixHandler struct {
w io.Writer
opts slog.HandlerOptions

groupBytes []byte
attrsBytes []byte
group string
groups []string
attrs []slog.Attr

lock *sync.Mutex
}
Expand Down Expand Up @@ -88,13 +89,7 @@ func (mh *mixHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
}

handler := *mh

if len(handler.groupBytes) > 0 {
group := string(mh.groupBytes)
handler.attrsBytes = mh.appendGroupAttrs(handler.attrsBytes, group, attrs)
} else {
handler.attrsBytes = mh.appendGroupAttrs(handler.attrsBytes, "", attrs)
}
handler.attrs = append(handler.attrs, attrs...)

return &handler
}
Expand All @@ -106,27 +101,52 @@ func (mh *mixHandler) WithGroup(name string) slog.Handler {
}

handler := *mh

if len(handler.groupBytes) > 0 {
handler.groupBytes = append(handler.groupBytes, groupConnector...)
handler.groupBytes = appendEscapedString(handler.groupBytes, name)
} else {
handler.groupBytes = appendEscapedString(handler.groupBytes, name)
if handler.group != "" {
handler.group = handler.group + groupConnector
}

handler.group = handler.group + name
handler.groups = append(handler.groups, name)

return &handler
}

func (mh *mixHandler) copyGroups(group string) []string {
cap := cap(mh.groups)
if group != "" {
cap += 1
}

groups := make([]string, 0, cap)
groups = append(groups, mh.groups...)

if group != "" {
groups = append(groups, group)
}

return groups
}

// Enabled reports whether the logger should ignore logs whose level is lower than passed level.
func (mh *mixHandler) Enabled(ctx context.Context, level slog.Level) bool {
return level >= mh.opts.Level.Level()
}

func (mh *mixHandler) appendKey(bs []byte, key string) []byte {
func (mh *mixHandler) appendKey(bs []byte, group string, key string) []byte {
if key == "" {
return bs
}

if mh.group != "" {
bs = appendEscapedString(bs, mh.group)
bs = append(bs, groupConnector...)
}

if group != "" {
bs = appendEscapedString(bs, group)
bs = append(bs, groupConnector...)
}

bs = appendEscapedString(bs, key)
bs = append(bs, keyValueConnector)

Expand Down Expand Up @@ -272,22 +292,30 @@ func (mh *mixHandler) appendAny(bs []byte, value any) []byte {
return bs
}

func (mh *mixHandler) appendAttr(bs []byte, attr slog.Attr) []byte {
func (mh *mixHandler) appendAttr(bs []byte, group string, attr slog.Attr) []byte {
kind := attr.Value.Kind()
replaceAttr := mh.opts.ReplaceAttr

if replaceAttr != nil && kind != slog.KindGroup {
groups := mh.copyGroups(group)
attr.Value = attr.Value.Resolve()
attr = replaceAttr(groups, attr)
}

// Resolve the Attr's value before doing anything else.
attr.Value = attr.Value.Resolve()

if attr.Equal(emptyAttr) {
return bs
}

kind := attr.Value.Kind()
if kind == slog.KindGroup {
bs = mh.appendGroupAttrs(bs, attr.Key, attr.Value.Group())
bs = mh.appendAttrs(bs, attr.Key, attr.Value.Group())

return bs
}

bs = mh.appendKey(bs, attr.Key)
bs = mh.appendKey(bs, group, attr.Key)

switch kind {
case slog.KindBool:
Expand All @@ -311,13 +339,9 @@ func (mh *mixHandler) appendAttr(bs []byte, attr slog.Attr) []byte {
return bs
}

func (mh *mixHandler) appendGroupAttrs(bs []byte, group string, attrs []slog.Attr) []byte {
for _, groupAttr := range attrs {
if group != "" {
groupAttr.Key = group + groupConnector + groupAttr.Key
}

bs = mh.appendAttr(bs, groupAttr)
func (mh *mixHandler) appendAttrs(bs []byte, group string, attrs []slog.Attr) []byte {
for _, attr := range attrs {
bs = mh.appendAttr(bs, group, attr)
}

return bs
Expand Down Expand Up @@ -357,11 +381,11 @@ func (mh *mixHandler) Handle(ctx context.Context, record slog.Record) error {
bs = mh.appendString(bs, record.Level.String())
bs = mh.appendString(bs, record.Message)
bs = mh.appendSource(bs, record.PC)
bs = mh.appendAttrs(bs, "", mh.attrs)

bs = append(bs, mh.attrsBytes...)
if record.NumAttrs() > 0 {
record.Attrs(func(attr slog.Attr) bool {
bs = mh.appendAttr(bs, attr)
bs = mh.appendAttr(bs, "", attr)
return true
})
}
Expand Down
7 changes: 6 additions & 1 deletion core/handler/mix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ func (d *demo) String() string {

// go test -v -cover -count=1 -test.cpu=1 -run=^TestMixHandler$
func TestMixHandler(t *testing.T) {
opts := &slog.HandlerOptions{}
opts := &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
t.Log(groups, a)
return a
},
}

//handler := NewMixHandler(os.Stdout, opts)
handler := slog.NewTextHandler(os.Stdout, opts)
Expand Down

0 comments on commit aaa3c40

Please sign in to comment.