Skip to content

Commit

Permalink
fix: barry 2024-12-17 21:54:59
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Dec 17, 2024
1 parent afafead commit f21e7dd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
54 changes: 38 additions & 16 deletions log/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"bytes"
"slices"
"unsafe"

"github.com/rs/zerolog"
Expand All @@ -17,23 +18,32 @@ func putEvent(e *Event)

func WithEvent(evt *Event) func(e *Event) {
return func(e *Event) {
defer putEvent(evt)
evt1 := convertEvent(evt)
if len(evt1.buf) == 0 {
if !e.Enabled() {
return
}

evt1.buf = bytes.TrimLeft(evt1.buf, "{")
evt1.buf = bytes.TrimRight(evt1.buf, ",")
if len(evt1.buf) == 0 {
buf := slices.Clone(convertEvent(evt).buf)
if len(buf) == 0 {
return
}

buf = bytes.TrimLeft(buf, "{")
buf = bytes.TrimSpace(bytes.Trim(buf, ","))
if len(buf) == 0 {
return
}

e1 := convertEvent(e)
if len(e1.buf) > 0 && len(evt1.buf) > 0 {
e1.buf = append(e1.buf, ',')
e1.buf = bytes.TrimSpace(e1.buf)
if len(e1.buf) == 0 {
e1.buf = append(e1.buf, '{')
e1.buf = append(e1.buf, buf...)
} else {
e1.buf = append(e1.buf, ","...)
e1.buf = append(e1.buf, buf...)
}
e1.buf = append(e1.buf, evt1.buf...)

e1.buf = bytes.TrimSpace(e1.buf)
}
}

Expand Down Expand Up @@ -63,19 +73,31 @@ func mergeEvent(to *Event, from ...*Event) *Event {
}

to1 := convertEvent(to)
to1.buf = bytes.TrimRight(to1.buf, ",")
to1.buf = bytes.TrimSpace(bytes.Trim(to1.buf, ","))
for i := range from {
if from[i] == nil {
continue
}

from1 := convertEvent(from[i])
from1.buf = bytes.TrimLeft(from1.buf, "{")
from1.buf = bytes.Trim(from1.buf, ",")
if len(from1.buf) > 0 {
to1.buf = append(to1.buf, ',')
to1.buf = append(to1.buf, from1.buf...)
buf := slices.Clone(convertEvent(from[i]).buf)
if len(buf) == 0 {
continue
}

buf = bytes.TrimLeft(buf, "{")
buf = bytes.TrimSpace(bytes.Trim(buf, ","))
if len(buf) == 0 {
continue
}

if len(to1.buf) == 0 {
to1.buf = append(to1.buf, '{')
to1.buf = append(to1.buf, buf...)
} else {
to1.buf = append(to1.buf, ","...)
to1.buf = append(to1.buf, buf...)
}
}
to1.buf = bytes.TrimSpace(to1.buf)
return to
}
22 changes: 4 additions & 18 deletions log/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import (
)

var (
logEnableChecker = func(ctx context.Context, lvl Level, nameOrMessage string, fields Map) bool { return true }
zErrMarshalFunc = zerolog.ErrorMarshalFunc
zInterfaceMarshalFunc = zerolog.InterfaceMarshalFunc
logGlobalHook = zerolog.HookFunc(func(e *zerolog.Event, level zerolog.Level, message string) {
logEnableChecker = func(ctx context.Context, lvl Level, nameOrMessage string, fields Map) bool { return true }
zErrMarshalFunc = zerolog.ErrorMarshalFunc
logGlobalHook = zerolog.HookFunc(func(e *zerolog.Event, level zerolog.Level, message string) {
if logEnableChecker == nil {
return
}
Expand All @@ -43,24 +42,11 @@ var (
}

if zErrMarshalFunc == nil {
return err.Error()
return fmt.Sprintf("%v", err)
}

return zErrMarshalFunc(err)
}

zerolog.InterfaceMarshalFunc = func(v any) ([]byte, error) {
if v == nil {
return []byte("null"), nil
}

switch e1 := v.(type) {
case json.Marshaler:
return e1.MarshalJSON()
}

return zInterfaceMarshalFunc(v)
}
})

// stdZeroLog default zerolog for debug
Expand Down

0 comments on commit f21e7dd

Please sign in to comment.