Created
April 17, 2020 14:38
-
-
Save Aoi-hosizora/705b43a80f6ec15de3c3eea7761ab0fb to your computer and use it in GitHub Desktop.
redigo redis logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package database | |
import ( | |
"fmt" | |
"github.com/gomodule/redigo/redis" | |
"github.com/sirupsen/logrus" | |
) | |
type RedisLogger struct { | |
conn redis.Conn | |
logger *logrus.Logger | |
} | |
func NewRedisLogger(conn redis.Conn, logger *logrus.Logger) *RedisLogger { | |
return &RedisLogger{conn: conn, logger: logger} | |
} | |
func (r *RedisLogger) render(cmd string, args []interface{}) string { | |
out := cmd | |
for _, arg := range args { | |
out += " " + fmt.Sprintf("%v", arg) | |
} | |
return out | |
} | |
func (r *RedisLogger) Do(commandName string, args ...interface{}) (reply interface{}, err error) { | |
reply, err = r.conn.Do(commandName, args...) | |
cmd := r.render(commandName, args) | |
if err == nil { | |
r.logger.WithFields(logrus.Fields{ | |
"Module": "redis", | |
"Command": cmd, | |
"Error": err, | |
}).Info(fmt.Sprintf("[Redis] return: %8T | %s", reply, cmd)) | |
} else { | |
r.logger.WithFields(logrus.Fields{ | |
"Module": "redis", | |
"Command": cmd, | |
"Error": err, | |
}).Error(fmt.Sprintf("[Redis] error: %v | %s", err, cmd)) | |
} | |
return | |
} | |
func (r *RedisLogger) Close() error { | |
return r.conn.Close() | |
} | |
func (r *RedisLogger) Err() error { | |
return r.conn.Err() | |
} | |
func (r *RedisLogger) Send(commandName string, args ...interface{}) error { | |
return r.conn.Send(commandName, args...) | |
} | |
func (r *RedisLogger) Flush() error { | |
return r.conn.Flush() | |
} | |
func (r *RedisLogger) Receive() (reply interface{}, err error) { | |
return r.conn.Receive() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment