-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
你好,请问一下,gorm2.0的日志怎么整合? #29
Comments
你好,我看了一下gorm的日志依赖,抽了点时间写了个demo,希望能帮到你
首先,更新一下 go-logger 最新的依赖程序。以下是demo程序
package main
import (
"context"
"fmt"
"time"
logging "github.com/donnie4w/go-logger/logger"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func main() {
if 1 == 2 {
gorm.Open(mysql.Open("test"), &gorm.Config{
Logger: newLogger(), //转换为go-logger
})
}
//测试打印效果:
l := newLogger()
l.Info(context.Background(), "sql1=", 1, 2)
l.Info(context.Background(), "sql2=")
// 打印结果:
// [INFO]2023/08/06 12:14:12 main.go:49: sql1=1 2
// [INFO]2023/08/06 12:14:12 main.go:51: sql2=
}
func newLogger() *log {
l := &log{}
l.log = logging.NewLogger() //根据需要调用设置日志文件函数
return l
}
type log struct {
log *logging.Logging
}
func (this *log) LogMode(level logger.LogLevel) (_r logger.Interface) {
//转换设置日志level
return
}
func (this *log) Info(ctx context.Context, s string, v ...interface{}) {
//根据需要打印接口的参数,由于参数个数不同,需要转换,比如用fmt.Append()
//this.log.Info(string(fmt.Append([]byte(s), v...)))
//也可以用下面方式,效率高一点
//打印格式根据需要调整,这里直接输出
if v != nil {
vs := make([]any, 1+len(v))
vs[0] = s
copy(vs[1:], v)
this.log.Info(vs...)
} else {
this.log.Info(s)
}
}
func (this *log) Warn(ctx context.Context, s string, v ...interface{}) {
//同上
}
func (this *log) Error(ctx context.Context, s string, v ...interface{}) {
//同上
}
func (this *log) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
//同上
}
***@***.***
…---- 回复的原邮件 ----
发件人
***@***.***>
发送日期
2023年8月6日 09:57
收件人
***@***.***>
抄送人
***@***.***>
主题
[donnie4w/go-logger] 你好,请问一下,gorm2.0的日志怎么整合? (Issue #29)
你好,请问一下,gorm2.0的日志怎么整合?
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>p, li { white-space: pre-wrap; }
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
你好,请问一下,gorm2.0的日志怎么整合?
The text was updated successfully, but these errors were encountered: