[TASK] add connection to sql log (fix #4)
This commit is contained in:
parent
ff299c42a7
commit
84f060cb3b
|
@ -126,4 +126,5 @@ __pycache__
|
||||||
|
|
||||||
|
|
||||||
# go project
|
# go project
|
||||||
|
config.conf
|
||||||
cmd/stock/config.conf
|
cmd/stock/config.conf
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/jinzhu/gorm/dialects/postgres"
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
|
|
||||||
|
"github.com/genofire/hs_master-kss-monolith/lib/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -17,23 +19,27 @@ type Config struct {
|
||||||
Type string
|
Type string
|
||||||
Connection string
|
Connection string
|
||||||
ReadConnection string
|
ReadConnection string
|
||||||
Logging bool
|
Logging bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func Open(c Config) (err error) {
|
func Open(c Config) (err error) {
|
||||||
|
writeLog := log.Log.WithField("db", "write")
|
||||||
config = &c
|
config = &c
|
||||||
Write, err = gorm.Open(config.Type, config.Connection)
|
Write, err = gorm.Open(config.Type, config.Connection)
|
||||||
Write.SingularTable(true)
|
Write.SingularTable(true)
|
||||||
Write.LogMode(c.Logging)
|
Write.LogMode(c.Logging)
|
||||||
|
Write.SetLogger(writeLog)
|
||||||
Write.Callback().Create().Remove("gorm:update_time_stamp")
|
Write.Callback().Create().Remove("gorm:update_time_stamp")
|
||||||
Write.Callback().Update().Remove("gorm:update_time_stamp")
|
Write.Callback().Update().Remove("gorm:update_time_stamp")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(config.ReadConnection) > 0 {
|
if len(config.ReadConnection) > 0 {
|
||||||
|
readLog := log.Log.WithField("db", "read")
|
||||||
Read, err = gorm.Open(config.Type, config.ReadConnection)
|
Read, err = gorm.Open(config.Type, config.ReadConnection)
|
||||||
Read.SingularTable(true)
|
Read.SingularTable(true)
|
||||||
Read.LogMode(c.Logging)
|
Read.LogMode(c.Logging)
|
||||||
|
Read.SetLogger(readLog)
|
||||||
Read.Callback().Create().Remove("gorm:update_time_stamp")
|
Read.Callback().Create().Remove("gorm:update_time_stamp")
|
||||||
Read.Callback().Update().Remove("gorm:update_time_stamp")
|
Read.Callback().Update().Remove("gorm:update_time_stamp")
|
||||||
} else {
|
} else {
|
||||||
|
|
Reference in New Issue