乐趣区

关于golang:工具库系列之Golang日志库非常简单的记录一切

Golang 日志库,非常简单的记录所有





感激 Uber 的开源我的项目 ZapLog,它的速度很快,十分快,且是工业级别利用于很多大型的生产环境。我从没见过这么快的日志库,于是我封装了一层,奥利给。

  1. 反对日志输入到控制台或文件,反对很多配置,如打印函数调用行,配置日志级别等。
  2. 反对日志层级输入到不同文件,且反对文件切割,革除过期日志文件等。
  3. 应用接口契约,你能够自行再次封装该库,我留有 SetCallerSkip 给你持续封装。

应用起来非常简单,你值得领有,快点用起来吧,代替你那又慢又难用的日志库。

English README

如何应用

非常简单,您只须要惯例执行:

go get -v -u github.com/hunterhug/golog

例子

默认什么都不配置的话,日志是打印到终端控制台的,默认日志级别是 InfoLevel,会打印出函数调用者的门路,默认长门路,打印进去的日志是文本格式,高亮模式。

您能够批改某些配置,来进行定制化,比方批改输入调用者函数门路为短门路,批改打印出 JSON 格局等等。

例子 1:默认用法

package main

import . "github.com/hunterhug/golog"

func main() {
    // use default log
    Info("now is Info", 2, "good")
    Debug("now is Debug", 2, "good")
    Warn("now is Warn", 2, "good")
    Error("now is Error", 2, "good")
    Infof("now is Infof: %d,%s", 2, "good")
    Debugf("now is Debugf: %d,%s", 2, "good")
    Warnf("now is Warnf: %d,%s", 2, "good")
    Errorf("now is Errorf: %d,%s", 2, "good")
    Sync()

    // config log
    SetLevel(DebugLevel).SetCallerShort(true).SetOutputJson(true).InitLogger()

    Info("now is Info", 2, "good")
    Debug("now is Debug", 2, "good")
    Warn("now is Warn", 2, "good")
    Error("now is Error", 2, "good")
    Infof("now is Infof: %d,%s", 2, "good")
    Debugf("now is Debugf: %d,%s", 2, "good")
    Warnf("now is Warnf: %d,%s", 2, "good")
    Errorf("now is Errorf: %d,%s", 2, "good")
    Sync()}

输入是:

2021-08-27T11:16:10.455+0800    INFO    /Users/pika/Documents/code/github/golog/demo/demo1/main.go:7    main.main       now is Info2 good
2021-08-27T11:16:10.455+0800    WARN    /Users/pika/Documents/code/github/golog/demo/demo1/main.go:9    main.main       now is Warn2 good
2021-08-27T11:16:10.455+0800    ERROR   /Users/pika/Documents/code/github/golog/demo/demo1/main.go:10   main.main       now is Error2 good
2021-08-27T11:16:10.455+0800    INFO    /Users/pika/Documents/code/github/golog/demo/demo1/main.go:11   main.main       now is Infof: 2,good
2021-08-27T11:16:10.455+0800    WARN    /Users/pika/Documents/code/github/golog/demo/demo1/main.go:13   main.main       now is Warnf: 2,good
2021-08-27T11:16:10.455+0800    ERROR   /Users/pika/Documents/code/github/golog/demo/demo1/main.go:14   main.main       now is Errorf: 2,good
{"l":"info","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:19","func":"main.main","msg":"now is Info2 good"}
{"l":"debug","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:20","func":"main.main","msg":"now is Debug2 good"}
{"l":"warn","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:21","func":"main.main","msg":"now is Warn2 good"}
{"l":"error","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:22","func":"main.main","msg":"now is Error2 good"}
{"l":"info","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:23","func":"main.main","msg":"now is Infof: 2,good"}
{"l":"debug","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:24","func":"main.main","msg":"now is Debugf: 2,good"}
{"l":"warn","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:25","func":"main.main","msg":"now is Warnf: 2,good"}
{"l":"error","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:26","func":"main.main","msg":"now is Errorf: 2,good"}

例子 2:输入到文件

最重要的是,您能够输入日志到文件中,文件还反对按日志级别输入到不同的文件中,且反对文件切割,文件主动清理等。

package main

import (
    "context"
    "fmt"
    . "github.com/hunterhug/golog"
    "time"
)

func main() {SetName("log_demo")
    SetLevel(InfoLevel)

    SetCallerShort(true).SetOutputJson(true)

    AddFieldFunc(func(ctx context.Context, m map[string]interface{}) {m["diy_filed"] = ctx.Value("diy")
    })

    SetOutputFile("./log", "demo").SetFileRotate(30*24*time.Hour, 24*time.Hour)
    SetIsOutputStdout(true)
    InitLogger()

    Info("now is Info", 2, "good")
    Debug("now is Debug", 2, "good")
    Warn("now is Warn", 2, "good")
    Error("now is Error", 2, "good")
    Infof("now is Infof: %d,%s", 2, "good")
    Debugf("now is Debugf: %d,%s", 2, "good")
    Warnf("now is Warnf: %d,%s", 2, "good")
    Errorf("now is Errorf: %d,%s", 2, "good")

    ctx := context.WithValue(context.Background(), "diy", []interface{}{"ahhahahahahh"})
    InfoContext(ctx, "InfoContext")
    InfoContext(ctx, "InfoContext, %s:InfoContext, %d", "ss", 333)
    InfoWithFields(map[string]interface{}{"k1": "sss"}, "InfoWithFields:%s,%d", "sss", 33333)
    InfoWithFields(map[string]interface{}{"k1": "sss"}, "InfoWithFields")

    err := Sync()
    if err != nil {fmt.Println(err.Error())
    }
}

您能够看到目录下有个文件夹 ·/log,外面就是日志了,能够关上看看。你会很好奇,控制台也打印出了日志,因为咱们同时配置了日志输入到终端,应用 SetIsOutputStdout 函数即可。

用法一览

学习这个库非常简单,看看上面的接口办法。

type LoggerInterface interface {SetOutputFile(logPath, fileName string)
    SetFileRotate(fileMaxAge, fileRotation time.Duration)
    SetLevel(level Level)
    SetCallerShort(short bool)
    SetName(name string)
    SetIsOutputStdout(isOutputStdout bool)
    SetCallerSkip(skip int)
    SetOutputJson(json bool)

    GetOutputFile() (logPath, fileName string)
    GetFileRotate() (fileMaxAge, fileRotation time.Duration)
    GetLevel() (level Level)
    GetCallerShort() (short bool)
    GetName() (name string)
    GetIsOutputStdout() (isOutputStdout bool)
    GetCallerSkip() (skip int)
    GetOutputJson() bool

    InitLogger()
    Sync() error

    Panicf(template string, args ...interface{})
    Fatalf(template string, args ...interface{})
    Errorf(template string, args ...interface{})
    Warnf(template string, args ...interface{})
    Infof(template string, args ...interface{})
    Debugf(template string, args ...interface{})

    Panic(args ...interface{})
    Fatal(args ...interface{})
    Error(args ...interface{})
    Warn(args ...interface{})
    Info(args ...interface{})
    Debug(args ...interface{})

    PanicWithFields(fields map[string]interface{}, template string, args ...interface{})
    FatalWithFields(fields map[string]interface{}, template string, args ...interface{})
    ErrorWithFields(fields map[string]interface{}, template string, args ...interface{})
    WarnWithFields(fields map[string]interface{}, template string, args ...interface{})
    InfoWithFields(fields map[string]interface{}, template string, args ...interface{})
    DebugWithFields(fields map[string]interface{}, template string, args ...interface{})

    PanicContext(ctx context.Context, template string, args ...interface{})
    FatalContext(ctx context.Context, template string, args ...interface{})
    ErrorContext(ctx context.Context, template string, args ...interface{})
    WarnContext(ctx context.Context, template string, args ...interface{})
    InfoContext(ctx context.Context, template string, args ...interface{})
    DebugContext(ctx context.Context, template string, args ...interface{})

    PanicContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
    FatalContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
    ErrorContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
    WarnContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
    InfoContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
    DebugContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})

    AddFieldFunc(func(context.Context, map[string]interface{}))
}

有些看不懂没关系,因为你可能用不上。

License

Copyright [2021-2021] [github.com/hunterhug]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
退出移动版