loggerhelper

package module
v2.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2022 License: MIT Imports: 6 Imported by: 26

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")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOpts = Options{
	Type:       FormatType_JSON,
	TimeFormat: time.RFC3339Nano,
	Level:      logrus.DebugLevel,
	DefaultFieldMap: logrus.FieldMap{
		logrus.FieldKeyTime:        "time",
		logrus.FieldKeyLevel:       "level",
		logrus.FieldKeyMsg:         "event",
		logrus.FieldKeyLogrusError: "logrus_error",
		logrus.FieldKeyFunc:        "caller",
		logrus.FieldKeyFile:        "file",
	},
	ExtFields: map[string]interface{}{},
	Hooks:     []logrus.Hook{},
}

Functions

func AddExtField

func AddExtField(field string, value interface{}) optparams.Option[Options]

AddExtField SetLogger函数的参数,用于增加扩展字段

func AddHooks

func AddHooks(hooks ...logrus.Hook) optparams.Option[Options]

AddHooks SetLogger函数的参数,用于增加钩子

func Debug

func Debug(message string, fields ...map[string]interface{})

Debug 默认log打印Debug级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func Error

func Error(message string, fields ...map[string]interface{})

Error 默认log打印Error级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func Fatal

func Fatal(message string, fields ...map[string]interface{})

Fatal 默认log打印Fatal级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func GetLogger

func GetLogger() *logrus.Logger

GetLogger 获取模块维护得logrus.Logger对象 该接口用于导出logger给其他模块使用

func Info

func Info(message string, fields ...map[string]interface{})

Info 默认log打印Info级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func New

func New() *logrus.Logger

New 初始化log的配置

func Panic

func Panic(message string, fields ...map[string]interface{})

Panic 默认log打印Panic级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func Set

func Set(opts ...optparams.Option[Options])

Set 设置logger @params opts ...Option 初始化使用的参数,具体可以看options.go文件

func Trace

func Trace(message string, fields ...map[string]interface{})

Trace 默认log打印Trace级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func Warn

func Warn(message string, fields ...map[string]interface{})

Warn 默认log打印Warn级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func WithAddExtFields

func WithAddExtFields(extFields map[string]interface{}) optparams.Option[Options]

WithAddExtFields SetLogger函数的参数,用于添加设置扩展字段

func WithDefaultFieldMap

func WithDefaultFieldMap(fm logrus.FieldMap) optparams.Option[Options]

WithDefaultFieldMap SetLogger函数的参数,用于设置默认字段的新命名

func WithDisableTimeField

func WithDisableTimeField() optparams.Option[Options]

WithDisableTimeField SetLogger函数的参数,用于设置使用text格式替换json格式

func WithExtFields

func WithExtFields(extFields map[string]interface{}) optparams.Option[Options]

WithExtFields SetLogger函数的参数,用于重置扩展字段

func WithLevel

func WithLevel(loglevel string) optparams.Option[Options]

WithLevel SetLogger函数的参数,用于设置log等级

func WithOutput

func WithOutput(writer io.Writer) optparams.Option[Options]

WithOutput SetLogger函数的参数,用于设置log的写入io

func WithTextFormat

func WithTextFormat() optparams.Option[Options]

WithTextFormat SetLogger函数的参数,用于设置使用text格式替换json格式

func WithTimeFormat

func WithTimeFormat(TimeFormat string) optparams.Option[Options]

WithTimeFormat SetLogger函数的参数,用于设置使用指定的时间解析格式,默认为RFC3339Nano

Types

type Dict

type Dict map[string]interface{}

Dict 简化键值对的写法

type FormatType

type FormatType int32

redis类型

const (
	FormatType_JSON FormatType = 0
	FormatType_Text FormatType = 1
)

type Log

type Log struct {
	// contains filtered or unexported fields
}

针对不同业务可以设置不同的Log对象 它是固定了一些字段的logger

func Export

func Export() *Log

Export 导出Log对象 该函数用于导出当前的默认logrus.Entry给特定模块使用

func (*Log) Debug

func (lg *Log) Debug(message string, fields ...map[string]interface{})

Debug 打印Debug级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func (*Log) Error

func (lg *Log) Error(message string, fields ...map[string]interface{})

Error 打印Error级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func (*Log) Fatal

func (lg *Log) Fatal(message string, fields ...map[string]interface{})

Fatal 打印Fatal级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func (*Log) GetLogger added in v2.0.2

func (lg *Log) GetLogger() *logrus.Entry

GetLogger 获取模块维护得logrus.Logger对象 该接口用于导出logger给其他模块使用

func (*Log) Info

func (lg *Log) Info(message string, fields ...map[string]interface{})

Info 打印Info级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func (*Log) Panic

func (lg *Log) Panic(message string, fields ...map[string]interface{})

Panic 打印Panic级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func (*Log) Trace

func (lg *Log) Trace(message string, fields ...map[string]interface{})

Trace 打印Trace级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

func (*Log) Warn

func (lg *Log) Warn(message string, fields ...map[string]interface{})

Warn 打印Warn级别信息 @params message string 事件消息 @params fields ...map[string]interface{} 信息字段

type Options

type Options struct {
	Type             FormatType
	DisableTimeField bool
	TimeFormat       string
	Level            logrus.Level
	DefaultFieldMap  logrus.FieldMap
	ExtFields        map[string]interface{}
	Output           io.Writer
	Hooks            []logrus.Hook
}

Option 设置key行为的选项 @attribute MaxTTL time.Duration 为0则不设置过期 @attribute AutoRefresh string 需要为crontab格式的字符串,否则不会自动定时刷新

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL