package main import ( "fmt" "time" "github.com/go-redis/redis" ) func main() { start := time.Now() client := redis.NewClient(&redis.Options{ Addr: "127.0.0.1:6379", }) err := client.Ping().Err() if err != nil { panic(err.Error()) } for i := 0; i < 1000000; i++ { now := time.Now() nowStr := fmt.Sprintf("%d-%d-%d %02d:%02d:%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()) client.LPush("log", nowStr+" 200 success laskdjflaskjdflkjasdl") } fmt.Println(time.Since(start)) }
package main import ( "fmt" "os" "time" ) func main() { file, err := os.Create("test.log") if err != nil { panic(err.Error()) } start := time.Now() for i := 0; i < 1000000; i++ { now := time.Now() nowStr := fmt.Sprintf("%d-%d-%d %02d:%02d:%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()) fmt.Fprintln(file, nowStr, "200", "success", "", "laksjdflkjsdal") } fmt.Println("time:", time.Since(start)) }
不对时间进行格式化处理时
对时间进行格式化处理后
可见redis和直接的fmt输出到文件,速度差了十倍左右
但用redis存日志查起来比较容易查,还可以将日期附加到key里区分每一天的日志,具体用文件存储还是redis按需求来
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:redis 存日志 - Python技术站