Skip to content

Commit

Permalink
fix: barry 2024-12-19 16:21:18
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Dec 19, 2024
2 parents 506b6ca + b41a291 commit 936ecc9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 40 deletions.
11 changes: 9 additions & 2 deletions async/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package async

import (
"context"
"fmt"
"runtime/debug"
"time"

"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/errors"
"github.com/pubgo/funk/generic"
"github.com/pubgo/funk/pretty"
"github.com/pubgo/funk/recovery"
"github.com/pubgo/funk/running"
"github.com/pubgo/funk/stack"
"github.com/pubgo/funk/try"
)
Expand Down Expand Up @@ -122,8 +124,13 @@ func logErr(fn interface{}, err error) {
return
}

if running.IsDebug {
debug.PrintStack()
errors.Debug(err)
}

logs.Err(err).
Str("func", stack.CallerWithFunc(fn).String()).
Str("err_stack", pretty.Sprint(err)).
Str("err_stack", fmt.Sprintf("%v", err)).
Msg(err.Error())
}
10 changes: 6 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ func LoadFromPath[T any](val *T, cfgPath string) {
assert.Must(yaml.Unmarshal(configBytes, val))

var getRealPath = func(pp []string) []string {
pp = lo.Map(pp, func(item string, index int) string { return filepath.Join(parentDir, item) })

var resPaths []string
for _, resPath := range pp {
pathList := listAllPath(resPath).Expect("failed to list cfgPath: %s", resPath)
resPaths = append(resPaths, pathList...)
}

// skip .cfg.yaml and cfg.other
resPaths = lo.Filter(resPaths, func(item string, index int) bool {
// skip .*.yaml and cfg.other
var cfgFilter = func(item string, index int) bool {
return strings.HasSuffix(item, "."+defaultConfigType) && !strings.HasPrefix(item, ".")
})
resPaths = lo.Map(resPaths, func(item string, index int) string { return filepath.Join(parentDir, item) })
}
resPaths = lo.Filter(resPaths, cfgFilter)
return lo.Uniq(resPaths)
}
var getCfg = func(resPath string) T {
Expand Down
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 @@ -44,24 +43,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 936ecc9

Please sign in to comment.