loggerhelper

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2021 License: MIT Imports: 4 Imported by: 20

README

loggerhelper

github.com/sirupsen/logrus的帮助程序.用于快速设置,开袋可用.

特性

  • 开袋可用,即便没有进行设置也已经设置为使用json格式打印消息.
  • WithExtFields可选,可以是0到任意多个
  • 可以通过log.Init(opts ...Option)设置默认logger
  • 支持hook

基本用法

即便不初始化也可以工作


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等级,默认字段的值,输出,以及钩子,初始化有两个方法:

  • Init(WithLevel(loglevel),...) 初始化默认的log对象

package main

import (
    log "github.com/Golang-Tools/loggerhelper"
    "io/ioutil"
    "os"
    "github.com/sirupsen/logrus"
    "github.com/sirupsen/logrus/hooks/writer"
)
func main() {
    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.Init(WithLevel("WARN"), WithExtFields(log.Dict{"d": 3}), WithOutput(ioutil.Discard), AddHooks(hook))
    log.Info("test")
    log.Warn("qweqwr", log.Dict{"a": 1})
}

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{},
}
View Source
var Logger = New()

Logger 默认的logger

Functions

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 Info

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

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

func Init

func Init(opts ...Option)

Init 初始化默认的log对象 @params opts ...Option 初始化使用的参数,具体可以看options.go文件

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 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{} 信息字段

Types

type Dict

type Dict map[string]interface{}

Dict 简化键值对的写法

type FormatType added in v0.0.4

type FormatType int32

redis类型

const (
	FormatType_JSON FormatType = 0
	FormatType_Text FormatType = 1
)

type Option added in v0.0.4

type Option interface {
	Apply(*Options)
}

Option configures how we set up the connection.

func AddExtField added in v0.0.4

func AddExtField(field string, value interface{}) Option

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

func AddHooks added in v0.0.4

func AddHooks(hooks ...logrus.Hook) Option

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

func WithDefaultFieldMap added in v0.0.4

func WithDefaultFieldMap(fm logrus.FieldMap) Option

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

func WithDisableTimeField added in v0.0.4

func WithDisableTimeField() Option

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

func WithExtFields added in v0.0.4

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

WithExtFields Init函数的参数,用于设置扩展字段

func WithLevel added in v0.0.4

func WithLevel(loglevel string) Option

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

func WithOutput added in v0.0.4

func WithOutput(writer io.Writer) Option

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

func WithTextFormat added in v0.0.4

func WithTextFormat() Option

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

func WithTimeFormat added in v0.0.4

func WithTimeFormat(TimeFormat string) Option

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

type Options added in v0.0.4

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格式的字符串,否则不会自动定时刷新

Jump to

Keyboard shortcuts

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