设置日志和时区
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package gormx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/sqlite"
|
||||
@@ -25,14 +25,13 @@ func NewGorm(opts ...Option) *gorm.DB {
|
||||
panic("mysql is not supported")
|
||||
}
|
||||
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=%s",
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True",
|
||||
defaultOpts.Mysql.User,
|
||||
defaultOpts.Mysql.Password,
|
||||
defaultOpts.Mysql.Host,
|
||||
defaultOpts.Mysql.Port,
|
||||
defaultOpts.Mysql.Database,
|
||||
defaultOpts.Charset,
|
||||
defaultOpts.Loc,
|
||||
)
|
||||
dialector = mysql.Open(dsn)
|
||||
|
||||
@@ -48,7 +47,10 @@ func NewGorm(opts ...Option) *gorm.DB {
|
||||
TablePrefix: defaultOpts.Prefix, // 表名前缀,`Article` 的表名应该是 `it_articles`
|
||||
SingularTable: defaultOpts.SingularTable, // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
|
||||
},
|
||||
Logger: NewGormxLogger(context.TODO()),
|
||||
NowFunc: func() time.Time {
|
||||
return time.Now().In(defaultOpts.Location) // 设置时区
|
||||
},
|
||||
Logger: NewGormxLogger(defaultOpts.Logger),
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@@ -12,12 +12,12 @@ import (
|
||||
// TODO:待进一步封装
|
||||
|
||||
type GormxLogger struct {
|
||||
*loggerx.Logger
|
||||
Logger loggerx.LoggerInterface
|
||||
}
|
||||
|
||||
func NewGormxLogger(ctx context.Context) *GormxLogger {
|
||||
func NewGormxLogger(logger loggerx.LoggerInterface) *GormxLogger {
|
||||
return &GormxLogger{
|
||||
Logger: loggerx.NewLogger(ctx),
|
||||
Logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+33
-7
@@ -1,13 +1,21 @@
|
||||
package gormx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/yuninks/loggerx"
|
||||
)
|
||||
|
||||
type clientOptions struct {
|
||||
Db string
|
||||
Mysql *MysqlOptions
|
||||
Sqlite *SqliteOptions
|
||||
Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles`
|
||||
SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
|
||||
Charset string // 使用utf8mb4编码
|
||||
Loc string // 时区设置 "Local" "Asia/Shanghai"
|
||||
Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles`
|
||||
SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
|
||||
Charset string // 使用utf8mb4编码
|
||||
Location *time.Location // 时区设置 "Local" "Asia/Shanghai"
|
||||
Logger loggerx.LoggerInterface // 日志记录器
|
||||
}
|
||||
|
||||
type MysqlOptions struct {
|
||||
@@ -25,8 +33,9 @@ type SqliteOptions struct {
|
||||
func defaultOptions() clientOptions {
|
||||
return clientOptions{
|
||||
SingularTable: true,
|
||||
Loc: "Local", //
|
||||
Location: time.Local, // 默认本地
|
||||
Charset: "utf8mb4",
|
||||
Logger: loggerx.NewLogger(context.Background()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +70,15 @@ func SetSqlite(dbPath string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// 使用postgres数据库
|
||||
func SetPostgres() Option {
|
||||
return func(o *clientOptions) {
|
||||
o.Db = "postgres"
|
||||
panic("postgres is not supported yet")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 表名前缀
|
||||
func SetTablePrefix(perfix string) Option {
|
||||
return func(o *clientOptions) {
|
||||
@@ -68,8 +86,16 @@ func SetTablePrefix(perfix string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func SetLoc(loc string) Option {
|
||||
// 设置时区,默认是本地
|
||||
func SetLocation(loc *time.Location) Option {
|
||||
return func(o *clientOptions) {
|
||||
o.Loc = loc
|
||||
o.Location = loc
|
||||
}
|
||||
}
|
||||
|
||||
// 设置日志记录器
|
||||
func SetLogger(logger loggerx.LoggerInterface) Option {
|
||||
return func(o *clientOptions) {
|
||||
o.Logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user