golang环境下的日志记录器-系列之二

前端之家收集整理的这篇文章主要介绍了golang环境下的日志记录器-系列之二前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本节工具类对loggor进行了异步封装,优化了日志输出时的线程并发,在高并发的场合适用,但会增加cpu负荷及数据落地时序有一定影响,使用接口无缝兼容loggor工具类


asyncLoggor.go

//@description	异步日志器,将同步日志器封装为异步
//@author chenbintao
//@data 2017-06-20	09:31	初稿
//	2017-06-30	11:31	调试

package loggor

type AsyncLogger struct {
	logger *Logger
}

func (this *AsyncLogger) init() *AsyncLogger {
	if nil == this.logger {
		this.logger = &Logger{}
	}
	return this
}

func (this *AsyncLogger) Sync(logger *Logger) *AsyncLogger {
	this.logger = logger
	return this
}

func (this *AsyncLogger) SetDebug(is_debug bool) {
	this.init()
	IS_DEBUG = is_debug
	this.logger.SetDebug(is_debug)
}

func (this *AsyncLogger) SetType(tp uint) {
	this.init()
	this.logger.SetType(tp)
}

func (this *AsyncLogger) SetRollingFile(dir,_file string,maxn int32,maxs int64,_u int64) {
	this.init()
	this.logger.SetRollingFile(dir,_file,maxn,maxs,_u)
}

func (this *AsyncLogger) SetRollingDaily(dir,_file string) {
	this.init()
	this.logger.SetRollingDaily(dir,_file)
}

func (this *AsyncLogger) SetRollingNormal(dir,_file string) {
	this.init()
	this.logger.SetRollingNormal(dir,_file)
}

func (this *AsyncLogger) Close() {
	if nil != this.logger {
		this.logger.Close()
	}
}
func (this *AsyncLogger) Println(a ...interface{}) {
	if nil != this.logger {
		go this.logger.Println(a...)
	}
}

func (this *AsyncLogger) Printf(format string,a ...interface{}) {
	if nil != this.logger {
		go this.logger.Printf(format,a...)
	}
}
原文链接:https://www.f2er.com/go/188207.html

猜你在找的Go相关文章