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按需求来
原文:http://www.cnblogs.com/cdyboke/p/7260715.html