From 8adc6cbf0b667153b13d7aa1bd7c9d58b424832d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BE=E9=87=8C=28barry=29?= Date: Tue, 17 Dec 2024 15:56:59 +0800 Subject: [PATCH 1/3] fix config path (#34) --- config/config.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 8f979fc..2cced3f 100644 --- a/config/config.go +++ b/config/config.go @@ -44,13 +44,14 @@ 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...) } resPaths = lo.Filter(resPaths, func(item string, index int) bool { return strings.HasSuffix(item, "."+defaultConfigType) }) - resPaths = lo.Map(resPaths, func(item string, index int) string { return filepath.Join(parentDir, item) }) return lo.Uniq(resPaths) } var getCfg = func(resPath string) T { From afafeada5a2d1ba0b9a8327ac3a6aec42ee3cd39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BE=E9=87=8C=28barry=29?= Date: Tue, 17 Dec 2024 17:24:35 +0800 Subject: [PATCH 2/3] add debug info (#35) --- async/go.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/async/go.go b/async/go.go index 1b110f1..455bfe3 100644 --- a/async/go.go +++ b/async/go.go @@ -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" ) @@ -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()) } From b41a291d0333cc07e46f82449d86be8e588ef1a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BE=E9=87=8C=28barry=29?= Date: Tue, 17 Dec 2024 22:00:16 +0800 Subject: [PATCH 3/3] fix log format (#36) --- log/event.go | 54 ++++++++++++++++++++++++++++++++++++--------------- log/global.go | 22 ++++----------------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/log/event.go b/log/event.go index 751a26d..ce45574 100644 --- a/log/event.go +++ b/log/event.go @@ -2,6 +2,7 @@ package log import ( "bytes" + "slices" "unsafe" "github.com/rs/zerolog" @@ -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) } } @@ -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 } diff --git a/log/global.go b/log/global.go index 88511c8..3ff890d 100644 --- a/log/global.go +++ b/log/global.go @@ -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 } @@ -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