This commit is contained in:
xxj
2023-08-07 16:44:53 +08:00
parent 792dc8235a
commit ec4de3fe10
5 changed files with 26 additions and 17 deletions

View File

@@ -23,6 +23,11 @@ type myLog interface {
TraceError(err error) error
Close()
Printf(string, ...interface{})
}
func GetLog() myLog {
return _log
}
// SetLog set log

View File

@@ -58,19 +58,19 @@ func (s *stdLog) Debugf(msg string, a ...interface{}) {
}
}
//Fatal 系统级错误
// Fatal 系统级错误
func (s *stdLog) Fatal(a ...interface{}) {
log.Output(2, color.Error.Render(getStr(a...)))
os.Exit(1)
}
//Fatalf 系统级错误
// Fatalf 系统级错误
func (s *stdLog) Fatalf(msg string, a ...interface{}) {
log.Output(2, color.Error.Render(fmt.Sprintf(msg, a...)))
os.Exit(1)
}
//JSON json输出
// JSON json输出
func (s *stdLog) JSON(a ...interface{}) {
for _, v := range a {
b, _ := json.MarshalIndent(v, "", " ")
@@ -94,3 +94,8 @@ func (s *stdLog) ErrorString(a ...interface{}) {
func (s *stdLog) Close() {
}
func (s *stdLog) Printf(msg string, a ...interface{}) {
log.Println(color.Error.Render(fmt.Sprintf(msg, a...)))
s.SaveError(fmt.Sprintf(msg, a...), "err")
}

View File

@@ -115,19 +115,19 @@ func (z *zapLog) Debugf(msg string, a ...interface{}) {
}
}
//Fatal 系统级错误
// Fatal 系统级错误
func (z *zapLog) Fatal(a ...interface{}) {
z.logger.Fatal(getStr(a...))
os.Exit(1)
}
//Fatalf 系统级错误
// Fatalf 系统级错误
func (z *zapLog) Fatalf(msg string, a ...interface{}) {
z.logger.Fatal(fmt.Sprintf(msg, a...))
os.Exit(1)
}
//JSON json输出
// JSON json输出
func (z *zapLog) JSON(a ...interface{}) {
for _, v := range a {
b, _ := json.MarshalIndent(v, "", " ")
@@ -145,3 +145,8 @@ func (z *zapLog) TraceError(err error) error {
// Close close the logger
func (z *zapLog) Close() {
}
func (z *zapLog) Printf(msg string, a ...interface{}) {
z.logger.Error(fmt.Sprintf(msg, a...))
z.SaveError(fmt.Sprintf(msg, a...), "err")
}

View File

@@ -93,21 +93,15 @@ func NewRedis(con *MyRedis) (dial RedisDial, err error) {
Dial: func() (redis.Conn, error) {
con.mtx.Lock()
defer con.mtx.Unlock()
var _con redis.Conn
var err error
index := con.conf.addrIdex
_con, err = redis.Dial("tcp", con.conf.addrs[index], redis.DialClientName(con.conf.clientName),
redis.DialConnectTimeout(con.conf.timeout), redis.DialDatabase(con.conf.db),
redis.DialPassword(con.conf.pwd), redis.DialReadTimeout(con.conf.readTimeout), redis.DialWriteTimeout(con.conf.writeTimeout),
)
if err != nil {
mylog.Error(err)
}
len := len(con.conf.addrs)
con.conf.addrIdex = (index + 1) % len
return _con, err
return redis.Dial("tcp", con.conf.addrs[index], redis.DialClientName(con.conf.clientName),
redis.DialConnectTimeout(con.conf.timeout), redis.DialDatabase(con.conf.db),
redis.DialPassword(con.conf.pwd), redis.DialReadTimeout(con.conf.readTimeout), redis.DialWriteTimeout(con.conf.writeTimeout),
)
},
Wait: true,
TestOnBorrow: func(c redis.Conn, t time.Time) error {

View File

@@ -24,6 +24,7 @@ func (mc *redisConPool) Destory() {
func (mc *redisConPool) GetRedisClient() redis.Conn {
if mc.pool == nil { // 创建连接
mc.mtx.Lock()
defer mc.mtx.Unlock()
mc.pool = &redis.Pool{
MaxIdle: mc.conf.maxIdle,
MaxActive: mc.conf.maxActive,
@@ -41,7 +42,6 @@ func (mc *redisConPool) GetRedisClient() redis.Conn {
return nil
},
}
mc.mtx.Unlock()
}
return mc.pool.Get()