Categorygithub.com/Golang-Tools/loggerhelper
modulepackage
0.0.4
Repository: https://github.com/golang-tools/loggerhelper.git
Documentation: pkg.go.dev

# 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})
}

# Functions

AddExtField Init函数的参数,用于增加扩展字段.
AddHooks Init函数的参数,用于增加钩子.
Debug 默认log打印Debug级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
Error 默认log打印Error级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
Fatal 默认log打印Fatal级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
Info 默认log打印Info级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
Init 初始化默认的log对象@params opts ...Option 初始化使用的参数,具体可以看options.go文件.
New 初始化log的配置.
Panic 默认log打印Panic级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
Trace 默认log打印Trace级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
Warn 默认log打印Warn级别信息@params message string 事件消息@params fields ...map[string]interface{} 信息字段.
WithDefaultFieldMap Init函数的参数,用于设置默认字段的新命名.
WithDisableTimeField Init函数的参数,用于设置使用text格式替换json格式.
WithExtFields Init函数的参数,用于设置扩展字段.
WithLevel Init函数的参数,用于设置log等级.
WithOutput Init函数的参数,用于设置log的写入io.
WithTextFormat Init函数的参数,用于设置使用text格式替换json格式.
WithTimeFormat Init函数的参数,用于设置使用指定的时间解析格式,默认为RFC3339Nano.

# Constants

No description provided by the author
No description provided by the author

# Variables

No description provided by the author
Logger 默认的logger.

# Structs

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

# Interfaces

Option configures how we set up the connection.

# Type aliases

Dict 简化键值对的写法.
redis类型.