40

k3log:一款开箱即用且高效、快捷、安全的 Golang 日志

 4 years ago
source link: https://www.tuicool.com/articles/VFfQben
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

k3log

一款开箱即用且高效,快捷,安全的golang日志,基于uber zap
实现日志切割,日志过期时间,动态改变日志的打印级别

导航

介绍

取名Three King Log

  • 由uber zap 日志扩展而来
  • 实现分隔,异步,动态级别打印,json/txt
  • 以key-value形式打印日志,适合项目里使用
  • 加入Dump打印数据详细类型结构,融入 go-spew 调度利器

文档参考

安装

go get -u github.com/ThreeKing2018/k3log

日志级别

Debug
Info
Warn
Error
Panic
Fatal
Dump

设置参数

WithFilename
WithLogLevel
WithMaxSize
WithMaxAge
WithStacktrace
WithIsStdOut
WithProjectName
WithLogType

使用方法

  • 简单使用
Debug("debug日志", 1)
Info("info日志", 2)
Warn("warn日志", 3)
Error("error日志", 4)
Panic("panic", 5)
Fatal("fatal", 6)
Dump("dump", 7)
  • 开发使用
NewDevelopment("dev", "log.txt")
defer Sync()
Info("Info", "dev")
  • 开发使用2
SetLogger(conf.WithIsStdOut(true),
		conf.WithLogType(conf.LogJsontype))
	Debug("self test", 100)
  • 开发使用3
SetLogger(conf.WithLogType(conf.LogJsontype), //打印json格式
    conf.WithProjectName("Zelog日志"),          //设置项目名称
    conf.WithFilename("log.txt"),             //设置输出文件名,或输出的路径
Debug("debug日志", 1)
Info("info日志", 2)
Warn("warn日志", 3)
Error("error日志", 4)
Panic("panic", 5)
Fatal("fatal", 6)
  • 生产使用
NewProducttion("pro", "log.txt")
defer Sync()
Error("pro", "ok")
  • 生产使用2
SetLogger(conf.WithLogType(conf.LogJsontype), //打印json格式
		conf.WithProjectName("Zelog日志"),          //设置项目名称
		conf.WithFilename("log.txt"),             //设置输出文件名,或输出的路径
		conf.WithLogLevel(conf.InfoLevel),        //设置日志级别,默认debug
		conf.WithMaxAge(30),                      //日志保存天数,默认30天
		conf.WithMaxSize(512),                    //多少M进行分隔日志,默认100M
		conf.WithIsStdOut(false)) //是否同时输出控制台
defer Sync()
Debug("debug日志", 1)
Info("info日志", 2)
Warn("warn日志", 3)
Error("error日志", 4)
Panic("panic", 5)
Fatal("fatal", 6)

动态改变日志的打印级别

Info("aa", 11)
SetLogLevel(conf.InfoLevel)
Info("info", 100)
Warn("warn", 200)
SetLogLevel(conf.ErrorLevel)
Info("info-100", 300) //这个无法输出,因为上面设置日志级别为:error
Error("err", 400)

效率

runtime.GOMAXPROCS(runtime.NumCPU())
BenchmarkInfo-4   	  100000	     10776 ns/op
BenchmarkInfo-4   	  200000	     12442 ns/op

Dump的使用

  • 级别为:debug
type s struct {
    Name string
    Age int
}
SetLogger(conf.WithIsStdOut(true))
Dump("name", "dump", "s", s{Name:"k3", Age: 2})

//{"name": "(string) (len=4) \"dump\"", "s": "(k3log.s) { Name: (string) (len=2) \"k3\", Age: (int) 2}"}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK