|
|
@@ -5,10 +5,8 @@ import ( |
|
|
|
"database/sql" |
|
|
|
"fmt" |
|
|
|
_ "github.com/go-sql-driver/mysql" //必须导入mysql驱动,否则会panic |
|
|
|
"os" |
|
|
|
"time" |
|
|
|
"xorm.io/xorm" |
|
|
|
"xorm.io/xorm/log" |
|
|
|
) |
|
|
|
|
|
|
|
var Db *xorm.Engine |
|
|
@@ -17,7 +15,7 @@ var Db *xorm.Engine |
|
|
|
func InitDB(c *md.DBCfg) error { |
|
|
|
var ( |
|
|
|
err error |
|
|
|
f *os.File |
|
|
|
//f *os.File |
|
|
|
) |
|
|
|
//创建Orm引擎 |
|
|
|
if Db, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4", c.User, c.Psw, c.Host, c.Name)); err != nil { |
|
|
@@ -29,22 +27,22 @@ func InitDB(c *md.DBCfg) error { |
|
|
|
if err = Db.Ping(); err != nil { //尝试ping数据库 |
|
|
|
return err |
|
|
|
} |
|
|
|
if c.ShowLog { //根据配置文件设置日志 |
|
|
|
Db.ShowSQL(true) //设置是否打印sql |
|
|
|
Db.Logger().SetLevel(0) //设置日志等级 |
|
|
|
//修改日志文件存放路径文件名是%s.log |
|
|
|
path := fmt.Sprintf(c.Path, c.Name) |
|
|
|
f, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777) |
|
|
|
if err != nil { |
|
|
|
os.RemoveAll(c.Path) |
|
|
|
if f, err = os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
logger := log.NewSimpleLogger(f) |
|
|
|
logger.ShowSQL(true) |
|
|
|
Db.SetLogger(logger) |
|
|
|
} |
|
|
|
//if c.ShowLog { //根据配置文件设置日志 |
|
|
|
// Db.ShowSQL(true) //设置是否打印sql |
|
|
|
// Db.Logger().SetLevel(0) //设置日志等级 |
|
|
|
// //修改日志文件存放路径文件名是%s.log |
|
|
|
// path := fmt.Sprintf(c.Path, c.Name) |
|
|
|
// f, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777) |
|
|
|
// if err != nil { |
|
|
|
// os.RemoveAll(c.Path) |
|
|
|
// if f, err = os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777); err != nil { |
|
|
|
// return err |
|
|
|
// } |
|
|
|
// } |
|
|
|
// logger := log.NewSimpleLogger(f) |
|
|
|
// logger.ShowSQL(true) |
|
|
|
// Db.SetLogger(logger) |
|
|
|
//} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|