1 Star 0 Fork 0

kzangv/gsf-fof

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
mysql.go 2.77 KB
Copy Edit Raw Blame History
kzangv authored 2024-08-05 16:41 . fixed
package component
import (
"context"
"fmt"
"gitee.com/kzangv/gsf-fof/component/define"
"gitee.com/kzangv/gsf-fof/gsf"
glogger "gitee.com/kzangv/gsf-fof/logger"
"github.com/urfave/cli/v2"
"gorm.io/gorm/logger"
"strings"
"time"
)
type MySQL struct {
Cfg define.ComponentMysql
Clts map[string]*MySQLClient
Log MysqlLogger
}
type MysqlLoggerDefault struct {
log glogger.Interface
}
func (l MysqlLoggerDefault) Info(_ context.Context, format string, v ...interface{}) {
l.log.Info(format, v...)
}
func (l MysqlLoggerDefault) Warn(_ context.Context, format string, v ...interface{}) {
l.log.Warn(format, v...)
}
func (l MysqlLoggerDefault) Error(_ context.Context, format string, v ...interface{}) {
l.log.Error(format, v...)
}
func (c *MySQL) SetLogLevel(v int) {
// log leve
nVal := logger.Error
switch v {
case glogger.Info:
nVal = logger.Info
case glogger.Warn:
nVal = logger.Warn
case glogger.Error:
nVal = logger.Error
}
c.Log.level = nVal
}
func (c *MySQL) CliFlags() []cli.Flag {
fs := make([][]cli.Flag, 0, len(c.Clts)+1)
fs = append(fs, []cli.Flag{
&cli.IntFlag{Name: "mysql-threshold", Usage: "mysql slow threshold", Value: 300, Destination: &c.Cfg.SlowThreshold},
&cli.IntFlag{Name: "mysql-op-timeout", Usage: "mysql operation timeout", Value: 15, Destination: &c.Cfg.OpTimeout},
})
for name, clt := range c.Clts {
fs = append(fs, clt.CliFlags(name))
}
l := 0
for k := range fs {
l += len(fs[k])
}
ret := make([]cli.Flag, 0, l)
for k := range fs {
ret = append(ret, fs[k]...)
}
return ret
}
func (c *MySQL) Init(l glogger.Interface, cfg *gsf.AppConfig, _ *cli.Context) error {
// 设置日志等级
c.Log.SetLogger(MysqlLoggerDefault{log: l})
c.Log.slowThreshold = time.Duration(c.Cfg.SlowThreshold) * time.Millisecond
switch cfg.Env() {
case gsf.EnvLocal:
c.SetLogLevel(glogger.Info)
case gsf.EnvDebug, gsf.EnvPrev, gsf.EnvProd:
c.SetLogLevel(glogger.Warn)
}
// 打印链接信息
for k, v := range c.Clts {
msg := fmt.Sprintf("Init MySQL (%s) config", k)
v.Init(cfg.Env(), c)
if define.PrintfDSN {
var slave string
cCfg := v.Config()
if len(cCfg.Slave) > 0 {
ss := make([]string, 0, len(cCfg.Slave))
for sk := range cCfg.Slave {
ss = append(ss, cCfg.Slave[sk].GetPrintDSN())
}
slave = "[" + strings.Join(ss, ";") + "]"
}
msg = fmt.Sprintf("%s (%s%s)", msg, cCfg.Master.GetPrintDSN(), slave)
}
l.WarnForce(msg)
}
return nil
}
func (c *MySQL) Run() error {
for name := range c.Clts {
if err := c.LoadOne(name); err != nil {
return err
}
}
return nil
}
func (c *MySQL) LoadOne(name string) (err error) {
if clt, ok := c.Clts[name]; ok {
err = clt.Load(name)
} else {
err = fmt.Errorf("MySQL (%s) config no find", name)
}
return err
}
func (c *MySQL) Close() error {
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kzangv/gsf-fof.git
git@gitee.com:kzangv/gsf-fof.git
kzangv
gsf-fof
gsf-fof
v0.4.3

Search