Go to file
kayos@tcp.direct e360fd8e14
Feat: add Logf
2024-02-13 17:32:39 -08:00
.gitignore zwrap init 2023-08-04 15:58:35 -07:00
LICENSE Update README.md and add LICENSE 2023-08-04 16:30:54 -07:00
README.md Update README.md and add LICENSE 2023-08-04 16:30:54 -07:00
go.mod zwrap init 2023-08-04 15:58:35 -07:00
go.sum zwrap init 2023-08-04 15:58:35 -07:00
wrap.go Feat: add Logf 2024-02-13 17:32:39 -08:00
wrap_test.go Feat: add Logf 2024-02-13 17:32:39 -08:00

zwrap

GoDoc Go Report Card

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