日志显示文件和行数

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
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())
}
}

View File

@ -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...)