Commit d597b804 authored by hujiebin's avatar hujiebin

feat:需要手动开启redis的hook

parent 62f557af
...@@ -14,6 +14,7 @@ const logDir = "/var/log/hilo/" ...@@ -14,6 +14,7 @@ const logDir = "/var/log/hilo/"
var filenamePrefix string var filenamePrefix string
var MyLog = logrus.New() var MyLog = logrus.New()
var RedisLog = logrus.New()
func Info(v interface{}) { func Info(v interface{}) {
MyLog.Info("") MyLog.Info("")
...@@ -44,6 +45,23 @@ func init() { ...@@ -44,6 +45,23 @@ func init() {
MyLog.SetReportCaller(true) MyLog.SetReportCaller(true)
} }
func InitRedisLog() {
RedisLog.SetOutput(os.Stdout)
RedisLog.SetFormatter(&logrus.TextFormatter{
ForceQuote: false,
DisableQuote: true,
TimestampFormat: "2006-01-02 15:04:05.000",
FullTimestamp: true,
})
hook := lfshook.NewHook(lfshook.WriterMap{
logrus.InfoLevel: GetRedisLog(),
}, &logrus.TextFormatter{ForceQuote: false, DisableQuote: true, TimestampFormat: time.RFC3339Nano})
RedisLog.AddHook(hook)
RedisLog.SetLevel(logrus.InfoLevel)
RedisLog.SetReportCaller(true)
}
func GetInfoLog() io.Writer { func GetInfoLog() io.Writer {
return getLevelWrite(logrus.InfoLevel) return getLevelWrite(logrus.InfoLevel)
} }
...@@ -91,3 +109,18 @@ func GetSqlLog() io.Writer { ...@@ -91,3 +109,18 @@ func GetSqlLog() io.Writer {
} }
return writer return writer
} }
func GetRedisLog() io.Writer {
var name string = "redis.log"
name = filenamePrefix + name
writer, err := rotatelogs.New(
name+".%Y%m%d%H",
rotatelogs.WithLinkName(name), // 生成软链,指向最新日志文件
rotatelogs.WithMaxAge(7*24*time.Hour), // 文件最大保存时间
rotatelogs.WithRotationTime(time.Hour), // 日志切割时间间隔
)
if err != nil {
MyLog.Fatal("Failed to create log file:", err.Error())
}
return writer
}
...@@ -26,10 +26,14 @@ func init() { ...@@ -26,10 +26,14 @@ func init() {
} else { } else {
mylogrus.MyLog.Info("redis db0 connection success - ", pong) mylogrus.MyLog.Info("redis db0 connection success - ", pong)
} }
// log hook
RedisClient.AddHook(redisHook{})
} }
func GetRedis() *redis.Client { func GetRedis() *redis.Client {
return RedisClient return RedisClient
} }
func AddRedisHook() {
// log hook
RedisClient.AddHook(redisHook{})
mylogrus.InitRedisLog()
}
...@@ -25,7 +25,7 @@ func (redisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error { ...@@ -25,7 +25,7 @@ func (redisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
if s, ok := start.(time.Time); ok { if s, ok := start.(time.Time); ok {
cost = time.Now().Sub(s).Milliseconds() cost = time.Now().Sub(s).Milliseconds()
} }
mylogrus.MyLog.Infof("redis cmd: <%s>,err:%v traceId:%v,userId:%v,cost:%v ms", cmd.String(), cmd.Err(), traceId, userId, cost) mylogrus.RedisLog.Infof("redis cmd: <%s>,err:%v traceId:%v,userId:%v,cost:%v ms", cmd.String(), cmd.Err(), traceId, userId, cost)
return nil return nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment