From 1f3ca994415b568d80d5d5f4207d63ac37023380 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 6 Apr 2023 21:13:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=98=BE=E7=A4=BA=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=92=8C=E8=A1=8C=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/pkg/logs/log.go | 21 ++++++++++++--------- model/query_test.go | 3 ++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/internal/pkg/logs/log.go b/internal/pkg/logs/log.go index 697c7a2..5dfa925 100644 --- a/internal/pkg/logs/log.go +++ b/internal/pkg/logs/log.go @@ -1,26 +1,29 @@ package logs import ( + "fmt" "log" + "runtime" "strings" ) func ErrPrintln(err error, desc string, args ...any) { - s := strings.Builder{} - tmp := "%s err:[%s]" - if desc == "" { - tmp = "%s%s" + if err == nil { + return } - s.WriteString(tmp) - argss := []any{desc, err} + var pcs [1]uintptr + runtime.Callers(2, pcs[:]) + f := runtime.CallersFrames([]uintptr{pcs[0]}) + ff, _ := f.Next() + s := strings.Builder{} + _, _ = fmt.Fprintf(&s, "%s:%d %s err:[%s]", ff.File, ff.Line, desc, err) if len(args) > 0 { s.WriteString(" args:") for _, arg := range args { - s.WriteString("%v ") - argss = append(argss, arg) + _, _ = fmt.Fprintf(&s, "%v", arg) } } if err != nil { - log.Printf(s.String(), argss...) + log.Println(s.String()) } } diff --git a/model/query_test.go b/model/query_test.go index 756558e..e82238c 100644 --- a/model/query_test.go +++ b/model/query_test.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "github.com/fthvgb1/wp-go/safety" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" "reflect" @@ -107,7 +108,7 @@ func init() { if err != nil { panic(err) } - glob = NewSqlxQuery(db, NewUniversalDb(func(ctx2 context.Context, a any, s string, a2 ...any) error { + glob = NewSqlxQuery(safety.NewVar(db), NewUniversalDb(func(ctx2 context.Context, a any, s string, a2 ...any) error { x := FormatSql(s, a2...) fmt.Println(x) return glob.Selects(ctx2, a, s, a2...)