You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
3 weeks ago | |
---|---|---|
.gitignore | 2 months ago | |
LICENSE | 2 months ago | |
README.md | 2 months ago | |
go.mod | 2 months ago | |
go.sum | 2 months ago | |
wrap.go | 3 weeks ago | |
wrap_test.go | 2 months ago |
README.md
zwrap
zwrap is a simple compatibility wrapper around zerolog that allows for a package to use many different logging libraries (including stdlib's log
package) without having to import them directly.
Usage
package main
import (
"os"
"log"
"git.tcp.direct/kayos/zwrap"
"github.com/rs/zerolog"
)
type aLogger interface {
Println(...interface{})
}
type needsLogger struct {
logger aLogger
}
func (n *needsLogger) SetLogger(logger aLogger) {
n.logger = logger
}
func (n *needsLogger) DoSomething() {
n.logger.Println("Hello, world!")
}
func main() {
// Create a new zerolog.Logger
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
// Demonstrate that we can use the stdlib logger
myThing := &needsLogger{}
myThing.SetLogger(log.New(os.Stdout, "", log.LstdFlags))
myThing.DoSomething()
// Demonstrate that we can use zerolog when wrapped
/* Before, does not compile:
myThing.SetLogger(logger)
myThing.DoSomething()
*/
// The zwrap solution, wrap the logger:
zl := zwrap.Wrap(logger)
myThing.SetLogger(zl)
myThing.DoSomething()
}