zlogwrap is a logging library, which wrapping all common use in My "Go App".
go get -u github.com/baac-tech/zlogwrap
func New(conf ...Config) zerologWrapper
Interface of functions
type zerologWrapper interface {
SetField(key string, anything interface{}) zerologWrapper // Set field in logs
Debug(anything ...interface{}) // level 0
Info(anything ...interface{}) // level 1
Warn(anything ...interface{}) // level 2
Error(anything ...interface{}) // level 3
Fatal(anything ...interface{}) // level 4
Panic(anything ...interface{}) // level 5
GetLogEvent(zerolog.Level) *zerolog.Event // With Caller (file:line)
}
Import the package
import (
"github.com/baac-tech/zlogwrap"
)
Using with these
logger := zlogwrap.New()
logger.Debug("A Debug Log")
// {"level":"debug","time":"2021-07-27T16:49:59+07:00","message":"A Debug Log"}
Optional: With configuration
logger := zlogwrap.New(zlogwrap.Config{
ServiceName: "zlogwrap",
})
logger.Debug("A Debug Log")
logger.SetField("key", "value").Debug("A Debug Log")
// {"level":"debug","service":"zlogwrap","time":"2021-08-05T11:14:00+07:00","message":"A Debug Log"}
// {"level":"debug","key":"value","service":"zlogwrap","time":"2021-08-05T11:14:00+07:00","message":"A Debug Log"}
or customize the "Logger" and this library support middleware RequestID
myLogger := zerolog.New(os.Stdout).
With().
Str("foo", "bar").
Float64("money", 10.99).
Logger()
logger := zlogwrap.New(zlogwrap.Config{
ServiceName: serviceName,
Logger: myLogger,
Context: c, // Which type is `*fiber.Ctx`
})
logger.Debug("A Debug Log")
// ps. see the real usage in `example/main.go`
// {"level":"debug","foo":"bar","money":10.99,"service":"zlogwrap","transaction_id":"b91f468c-c608-4729-88d8-9a12c951c31a","url":"/","message":"A Debug Log"}
// Config defines the config for library.
type Config struct {
// Optional. Default: false
Hidden bool
// Optional. Default: ""
ServiceName string
// Optional. Default: &fiber.Ctx{}
Context *fiber.Ctx
// Optional. Default: log.Logger
Logger zerolog.Logger
}
var ConfigDefault = Config{
Hidden: false,
ServiceName: "",
Context: nil,
Logger: log.Logger,
}
Please go to example/main.go