日志显示文件和行数

This commit is contained in:
xing 2023-04-06 21:13:02 +08:00
parent c09f54b9c0
commit 1f3ca99441
2 changed files with 14 additions and 10 deletions

View File

@ -1,26 +1,29 @@
package logs package logs
import ( import (
"fmt"
"log" "log"
"runtime"
"strings" "strings"
) )
func ErrPrintln(err error, desc string, args ...any) { func ErrPrintln(err error, desc string, args ...any) {
s := strings.Builder{} if err == nil {
tmp := "%s err:[%s]" return
if desc == "" {
tmp = "%s%s"
} }
s.WriteString(tmp) var pcs [1]uintptr
argss := []any{desc, err} 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 { if len(args) > 0 {
s.WriteString(" args:") s.WriteString(" args:")
for _, arg := range args { for _, arg := range args {
s.WriteString("%v ") _, _ = fmt.Fprintf(&s, "%v", arg)
argss = append(argss, arg)
} }
} }
if err != nil { if err != nil {
log.Printf(s.String(), argss...) log.Println(s.String())
} }
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/safety"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"reflect" "reflect"
@ -107,7 +108,7 @@ func init() {
if err != nil { if err != nil {
panic(err) 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...) x := FormatSql(s, a2...)
fmt.Println(x) fmt.Println(x)
return glob.Selects(ctx2, a, s, a2...) return glob.Selects(ctx2, a, s, a2...)