bizlogger是一款可输出CSV日志并按时间切分的日志模块,分割符可定制。goroutine安全。日志字符串拼接采用先计算总长度再一次性分配内存的方法来提升效率。按时间切分功能利用的是logrotator来实现的。经过在线上实际压测,QPS 6000的负载下能正常工作。

用法:

package main

import (
	"fmt"
	"github.com/pochard/bizlogger"
	"time"
)

func main() {
	// rollup every 20 minutes. And the log line is separated by ","
	logger, err := bizlogger.NewTabLogger("/data/web_log/click1-%Y%m%d-%H%M.log", 20*time.Minute, ",")
	if err != nil {
		fmt.Printf("%v", err)
		return
	}
	defer logger.Close()

	sl := make([]string, 4)
	sl[0] = "410001"
	sl[1] = "1583893679"
	sl[2] = "183.199.195.151"
	sl[3] = "C03FD5AAB4CA"

	for i := 0; i != 10; i++ {
		err := logger.Log(sl)
		if err != nil {
			fmt.Printf("%v", err)
		}
	}
}

输出日志:

click1-20200328-0920.log
click1-20200328-0940.log
click1-20200328-1000.log

日志内容:

410001,1583893679,183.199.195.151,C03FD5AAB4CA
410001,1583893679,183.199.195.151,C03FD5AAB4CA
410001,1583893679,183.199.195.151,C03FD5AAB4CA
410001,1583893679,183.199.195.151,C03FD5AAB4CA
410001,1583893679,183.199.195.151,C03FD5AAB4CA
410001,1583893679,183.199.195.151,C03FD5AAB4CA
410001,1583893679,183.199.195.151,C03FD5AAB4CA
410001,1583893679,183.199.195.151,C03FD5AAB4CA
410001,1583893679,183.199.195.151,C03FD5AAB4CA
410001,1583893679,183.199.195.151,C03FD5AAB4CA

相关文章:

《Go日志滚动模块logrotator的使用方法及性能》

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐