设置日志和时区

This commit is contained in:
Yun
2025-07-12 17:12:24 +08:00
parent 10ac02f3e4
commit f63e3ff098
3 changed files with 42 additions and 14 deletions
+33 -7
View File
@@ -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
}
}