From 7c4e17677510697d64d5fe33f0519e8baef562f1 Mon Sep 17 00:00:00 2001 From: bryan Date: Tue, 12 Nov 2024 00:42:57 +0800 Subject: [PATCH] add error log --- redis/redis.go | 7 +++++++ sentinel/sentinel.go | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/redis/redis.go b/redis/redis.go index 8f660e8..d6c7bc8 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -56,6 +56,10 @@ func NewPool(cfg Config) *redis.Pool { logStdWriter = os.Stdout logStdFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile logStdLogger = log.New(logStdWriter, logStdPrefix, logStdFlags) + logErrPrefix = "ERRO " + logErrWriter = os.Stderr + logErrFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile + logErrLogger = log.New(logErrWriter, logErrPrefix, logErrFlags) ) return &redis.Pool{ MaxIdle: *cfg.MaxIdle, @@ -65,6 +69,7 @@ func NewPool(cfg Config) *redis.Pool { Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", masterAddr) if err != nil { + logErrLogger.Output(2, fmt.Sprintf("connect [%s] error: %s", masterAddr, err)) return nil, err } @@ -76,8 +81,10 @@ func NewPool(cfg Config) *redis.Pool { } if err != nil { + logErrLogger.Output(2, fmt.Sprintf("auth [%s] error: %s", masterAddr, err)) return nil, fmt.Errorf("redis master AUTH failed: <%s>", err.Error()) } else if okstr != "OK" { + logErrLogger.Output(2, fmt.Sprintf("auth [%s] not return ok but '%s'", masterAddr, okstr)) return nil, fmt.Errorf("redis master AUTH failed: <%s>", okstr) } diff --git a/sentinel/sentinel.go b/sentinel/sentinel.go index e09ed97..70546b3 100644 --- a/sentinel/sentinel.go +++ b/sentinel/sentinel.go @@ -121,6 +121,10 @@ func NewPool(cfg Config) *redis.Pool { logStdWriter = os.Stdout logStdFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile logStdLogger = log.New(logStdWriter, logStdPrefix, logStdFlags) + logErrPrefix = "ERRO " + logErrWriter = os.Stderr + logErrFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile + logErrLogger = log.New(logErrWriter, logErrPrefix, logErrFlags) ) return &redis.Pool{ MaxIdle: *cfg.MaxIdle, @@ -130,11 +134,13 @@ func NewPool(cfg Config) *redis.Pool { Dial: func() (redis.Conn, error) { masterAddr, err := sntnl.MasterAddr() if err != nil { + logErrLogger.Output(2, fmt.Sprintf("get master from sentinel error: %s", err)) return nil, err } c, err := redis.Dial("tcp", masterAddr) if err != nil { + logErrLogger.Output(2, fmt.Sprintf("connect [%s] error: %s", masterAddr, err)) return nil, err } @@ -146,8 +152,10 @@ func NewPool(cfg Config) *redis.Pool { } if err != nil { + logErrLogger.Output(2, fmt.Sprintf("auth [%s] error: %s", masterAddr, err)) return nil, fmt.Errorf("redis master AUTH failed: <%s>", err.Error()) } else if okstr != "OK" { + logErrLogger.Output(2, fmt.Sprintf("auth [%s] not return ok but '%s'", masterAddr, okstr)) return nil, fmt.Errorf("redis master AUTH failed: <%s>", okstr) } @@ -156,6 +164,7 @@ func NewPool(cfg Config) *redis.Pool { err = fmt.Errorf( "master(%s) got by name '%s' is not redis master", masterAddr, masterName) + logErrLogger.Output(2, err.String()) return nil, err }