modulepackage
2.0.2
Repository: https://github.com/golang-tools/loggerhelper.git
Documentation: pkg.go.dev
# README
loggerhelper/V2
github.com/sirupsen/logrus
的帮助程序.用于快速设置,开袋可用.
该模块定位为基础组件,如果需要用它就只用它比较好.
V2版本是对V0版本的重构,允许修改全局logger,并允许将logger输出
V2版本针对go 1.18+,使用泛型语法.低版本还是继续使用v0版本
特性
- 开袋可用,即便没有进行设置也已经设置为使用json格式打印消息.
WithExtFields
可选,可以是0到任意多个- 可以通过
log.Set(opts ...optparams.Option[log.Options])
设置默认logger - 支持
hook
- 支持获取默认logger
- 支持导出Log对象针对不同业务进行区分.导出的Log对象只是默认字段不同,依然受log.Set设置的其他属性影响
基本用法
即便不初始化也可以工作
package main
import (
log "github.com/Golang-Tools/loggerhelper"
)
func main() {
log.Info("test")
log.Warn("qweqwr", log.Dict{"a": 1},log.Dict{"b": 1})
}
设置log
设置log可以设置log等级,默认字段的值,输出,以及钩子:
func Set(opts ...optparams.Option[Options])
设置logger对象
package main
import (
log "github.com/Golang-Tools/loggerhelper/v2"
"io/ioutil"
"os"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
)
func main() {
log.Info("test1")
log.Set(log.WithLevel("WARN"), log.WithExtFields(log.Dict{"d": 3}))
log.Info("test2")
log.Warn("test3")
log.Set(log.WithLevel("Debug"), log.WithExtFields(log.Dict{"e": 3}))
log.Debug("test4", log.Dict{"a": 1})
log.Warn("test5", log.Dict{"a": 1})
hook := writer.Hook{ // Send logs with level higher than warning to stderr
Writer: os.Stderr,
LogLevels: []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
},
}
log.SetLogger(WithLevel("WARN"), WithExtFields(log.Dict{"d": 3}), WithOutput(ioutil.Discard), AddHooks(hook))
log.Info("test")
log.Warn("qweqwr", log.Dict{"a": 1})
}
获取logger
获取logger接口GetLogger() *logrus.Logger
可以获取到当前的logger,这主要用于导出给其他模块使用.比如用于设置gin的log
app.Use(ginlogrus.Logger(log.GetLogger()), gin.Recovery())
导出Log
往往我们希望不同的模块使用不同的log标识这样可以比较准确地定位错误.这时可以通过Export
接口导出当前的Log对象.
log.Set(log.WithExtFields(log.Dict{"app": "l1"}))
Logger1 := log.Export()
log.Set(log.WithExtFields(log.Dict{"app": "l2"}))
Logger2 := log.Export()
Logger1.Debug("test logger1")
Logger2.Debug("test logger2")
# Packages
No description provided by the author
# Functions
AddExtField SetLogger函数的参数,用于增加扩展字段.
AddHooks SetLogger函数的参数,用于增加钩子.
Debug 默认log打印Debug级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
Error 默认log打印Error级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
Export 导出Log对象该函数用于导出当前的默认logrus.Entry给特定模块使用.
Fatal 默认log打印Fatal级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
GetLogger 获取模块维护得logrus.Logger对象该接口用于导出logger给其他模块使用.
Info 默认log打印Info级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
New 初始化log的配置.
Panic 默认log打印Panic级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
Set 设置logger@params opts ...Option 初始化使用的参数,具体可以看options.go文件.
Trace 默认log打印Trace级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
Warn 默认log打印Warn级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
WithAddExtFields SetLogger函数的参数,用于添加设置扩展字段.
WithDefaultFieldMap SetLogger函数的参数,用于设置默认字段的新命名.
WithDisableTimeField SetLogger函数的参数,用于设置使用text格式替换json格式.
WithExtFields SetLogger函数的参数,用于重置扩展字段.
WithLevel SetLogger函数的参数,用于设置log等级.
WithOutput SetLogger函数的参数,用于设置log的写入io.
WithTextFormat SetLogger函数的参数,用于设置使用text格式替换json格式.
WithTimeFormat SetLogger函数的参数,用于设置使用指定的时间解析格式,默认为RFC3339Nano.
# Constants
No description provided by the author
No description provided by the author
# Variables
No description provided by the author