Skip to content

使用 seelog 日志组件

Joe Zou edited this page Sep 16, 2020 · 1 revision

介绍

从v1.8.0+开始因开源协议原因去除seelog,改为自定义日志组件,如还想使用seelog查看日志请参考以下实现方式

引入seelog组件

gopm get github.com/cihub/seelog -v -g

或者

go get -u github.com/cihub/seelog

添加seelog.xml

将seelog.xml放置到项目根目录下

<seelog type="sync" mininterval="2000000" maxinterval="100000000" critmsgcount="500" minlevel="debug">

    <outputs formatid="all">
        <console formatid="fmterror"/>
    </outputs>
    <formats>
        <format id="fmtinfo" format="[%Level]  [%Time] %Msg%n"/>
        <format id="fmterror" format="[%LEVEL] [%Time] [%FuncShort @ %File.%Line] %Msg%n"/>
        <format id="all" format="[%Level] [%Time]  [@ %File.%Line] %Msg%n"/>
        <format id="criticalemail" format="Critical error on our server!\n    %Time %Date %RelFile %Func %Msg \nSent by Seelog"/>
    </formats>
</seelog>

编写代码

使用以下代码初始化seelog组件

func initSeeLog(configPath string) LoggerInterface {
	logger, err := seelog.LoggerFromConfigAsFile(configPath)

	//if error is happen change to default config.
	if err != nil {
		logger, err = seelog.LoggerFromConfigAsBytes([]byte("<seelog />"))
	}

	logger.SetAdditionalStackDepth(1)
	seelog.ReplaceLogger(logger)
	defer seelog.Flush()

	return logger
}

type DefaultLogger struct {
	log seelog.LoggerInterface
}

func (this *DefaultLogger) Debugf(format string, params ...interface{}) {
	this.Debug(format, params)
}

func (this *DefaultLogger) Infof(format string, params ...interface{}) {
	this.Debug(format, params)
}

func (this *DefaultLogger) Warnf(format string, params ...interface{}) {
	this.Debug(format, params)
}

func (this *DefaultLogger) Errorf(format string, params ...interface{}) {
	this.Debug(format, params)
}

func (this *DefaultLogger) Debug(v ...interface{}) {
	this.log.Debug(v)
}
func (this *DefaultLogger) Info(v ...interface{}) {
	this.Debug(v)
}

func (this *DefaultLogger) Warn(v ...interface{}) {
	this.Debug(v)
}

运行

func main() {
        loggerInterface:=initSeeLog("seelog.xml")
	agollo.SetLogger(&DefaultLogger{loggerInterface})
}