Files
public/mysqldb/log.go
xxjwxc 2d6b04f6c6 1
2022-06-19 11:01:09 +08:00

35 lines
821 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package mysqldb
import (
"log"
"time"
"github.com/xxjwxc/public/mylog"
"gorm.io/gorm/logger"
)
// DbLog ...
type DbLog struct {
}
// Write ...
func (lg DbLog) Write(p []byte) (n int, err error) {
mylog.SaveError(string(p), "sql")
return len(p), err
}
// GetDBlog 获取默认logger
func GetDBlog(ignoreRecordNotFoundError bool) logger.Interface {
newLogger := logger.New(
log.New(DbLog{}, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Error, // Log level
IgnoreRecordNotFoundError: ignoreRecordNotFoundError, // 忽略ErrRecordNotFound记录未找到错误
Colorful: false, // 禁用彩色打印
},
)
return newLogger
}