donnie4w / go-logger

High-performance Go logging library, with concurrency performance 10 times or more faster than the official library. Go高性能日志库,适用于高并发场景,并发性能是官方库10倍或以上
https://tlnet.top/logdoc
BSD 3-Clause "New" or "Revised" License
305 stars 103 forks source link

你好,请问一下,gorm2.0的日志怎么整合? #29

Open achuanok opened 1 year ago

achuanok commented 1 year ago

你好,请问一下,gorm2.0的日志怎么整合?

donnie4w commented 1 year ago
  你好,我看了一下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; }