refactor(logging): 迁移日志记录到 Zerolog 并优化错误处理
- 将日志记录库从 Logrus 替换为 Zerolog,提升性能和灵活性 - 更新各个模块的日志记录方式,确保一致性 - 优化错误处理逻辑,确保在发生错误时能够正确记录并传递错误信息 - 移除不再使用的错误处理字段,简化代码结构 - 更新相关文档以反映新的日志记录和错误处理机制
This commit is contained in:
Vendored
+16
-20
@@ -2,6 +2,7 @@ package cache
|
||||
|
||||
import (
|
||||
. "code.hoteas.com/golang/hotime/common"
|
||||
"code.hoteas.com/golang/hotime/log"
|
||||
"github.com/garyburd/redigo/redis"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -18,18 +19,14 @@ type CacheRedis struct {
|
||||
pool *redis.Pool
|
||||
tag int64
|
||||
ContextBase
|
||||
*Error
|
||||
Log *log.Logger
|
||||
initOnce sync.Once
|
||||
}
|
||||
|
||||
func (that *CacheRedis) GetError() *Error {
|
||||
|
||||
return that.Error
|
||||
|
||||
}
|
||||
|
||||
func (that *CacheRedis) SetError(err *Error) {
|
||||
that.Error = err
|
||||
func (that *CacheRedis) logErr(err error) {
|
||||
if err != nil && that.Log != nil {
|
||||
that.Log.Error().Err(err).Msg("redis cache error")
|
||||
}
|
||||
}
|
||||
|
||||
// 唯一标志
|
||||
@@ -97,7 +94,7 @@ func (that *CacheRedis) del(key string) {
|
||||
if del != -1 {
|
||||
val, err := redis.Strings(conn.Do("KEYS", key))
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
that.logErr(err)
|
||||
return
|
||||
}
|
||||
if len(val) == 0 {
|
||||
@@ -109,12 +106,12 @@ func (that *CacheRedis) del(key string) {
|
||||
}
|
||||
_, err = conn.Do("EXEC")
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
that.logErr(err)
|
||||
}
|
||||
} else {
|
||||
_, err := conn.Do("DEL", key)
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
that.logErr(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,7 +126,7 @@ func (that *CacheRedis) set(key string, value string, expireSeconds int64) {
|
||||
|
||||
_, err := conn.Do("SET", key, value, "EX", ObjToStr(expireSeconds))
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
that.logErr(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,7 +143,7 @@ func (that *CacheRedis) get(key string) *Obj {
|
||||
if err != nil {
|
||||
reData.Data = nil
|
||||
if !strings.Contains(err.Error(), "nil returned") {
|
||||
that.Error.SetError(err)
|
||||
that.logErr(err)
|
||||
}
|
||||
}
|
||||
return reData
|
||||
@@ -175,11 +172,10 @@ func (that *CacheRedis) Cache(key string, data ...interface{}) *Obj {
|
||||
tim += that.TimeOut
|
||||
}
|
||||
if len(data) == 2 {
|
||||
that.Error.SetError(nil)
|
||||
tempt := ObjToInt64(data[1], that.Error)
|
||||
tempt := ObjToInt64(data[1])
|
||||
if tempt > tim {
|
||||
tim = tempt
|
||||
} else if that.GetError() == nil {
|
||||
} else {
|
||||
tim = tim + tempt
|
||||
}
|
||||
}
|
||||
@@ -212,7 +208,7 @@ func (that *CacheRedis) CachesGet(keys []string) Map {
|
||||
values, err := redis.Strings(conn.Do("MGET", args...))
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "nil returned") {
|
||||
that.Error.SetError(err)
|
||||
that.logErr(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -257,7 +253,7 @@ func (that *CacheRedis) CachesSet(data Map, timeout ...int64) {
|
||||
}
|
||||
_, err := conn.Do("EXEC")
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
that.logErr(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,6 +277,6 @@ func (that *CacheRedis) CachesDelete(keys []string) {
|
||||
|
||||
_, err := conn.Do("DEL", args...)
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
that.logErr(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user