Go iris 日志文件的分割

Go iris 日志文件的分割,第1张

概述iris官方案例只给了文件日志,但没有给日志分割的方法,一旦访问量过大,日志文件就成吨成吨的,这很让我苦恼。经研究使用 github.com/lestrrat-go/file-rotatelogs包可解决,代码如下:packagemainimport( rotatelogs"github.com/lestrrat-go/file-rotatelogs" "os" "time" "g

iris 官方案例只给了文件日志,但没有给日志分割的方法,一旦访问量过大,日志文件就成吨成吨的,这很让我苦恼。经研究使用 github.com/lestrrat-go/file-rotatelogs 包可解决,代码如下:

package mainimport (	rotatelogs "github.com/lestrrat-go/file-rotatelogs"	"os"	"time"	"github.com/kataras/iris/v12"	"github.com/kataras/iris/v12/mIDdleware/logger")func main() {	app := iris.New()	path := "iris"	writer, _ := rotatelogs.New(		path+"%Y%m%d%H%M.log",		rotatelogs.Withlinkname(path),		rotatelogs.WithMaxAge(time.Duration(180)*time.Second),		//这里设置1分钟产生一个日志文件		rotatelogs.WithRotationTime(time.Duration(60)*time.Second),	)	app.Logger().Setoutput(writer)//日志写入文件	app.Logger().AddOutput(os.Stdout)//日志同时写入控制台,如果不想显示控制台可注释此语句	//记录路由日志	app.Use(logger.New(logger.Config{		Status:             true,		IP:                 true,		Method:             true,		Path:               true,		query:              true,		LogFunc:            nil,		LogFuncCtx:         nil,		Skippers:           nil,	}))	app.Get("/", func(ctx iris.Context) {		//手动日志		ctx.Application().Logger().Infof("这里产生一个错误,请注意了: %s", ctx.Path())		ctx.WriteString("hello")	})	if err := app.Run(iris.Addr(":8080")); err != nil {		app.Logger().Warn("Shutdown with error: " + err.Error())	}}

  

总结

以上是内存溢出为你收集整理的Go iris 日志文件的分割全部内容,希望文章能够帮你解决Go iris 日志文件的分割所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1244615.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存