This is a default Driver implementation for bridging slf4go and zap.
slf4go-zap dependents on slf4go and zap.
go get github.com/go-eden/slf4go-zapslf4go-zap focuses on bridging logs, you should configure zap according to your needs.
package main
import (
slog "github.com/go-eden/slf4go"
slogzap "github.com/go-eden/slf4go-zap"
"go.uber.org/zap"
)
func main() {
zapcfg = zap.Config{
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
Development: false,
// DisableCaller: true,
DisableStacktrace: true,
Encoding: "console",
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stdout"},
InitialFields: map[string]interface{}{"foo": "bar"},
}
cfg = slogzap.Config{
ZapConfig: &zapcfg,
ZapOptions: []zap.Option{
zap.AddCallerSkip(slogzap.SkipUntilTrueCaller), // 3
},
}
slogzap.Init(&cfg)
// use the global logger
slog.Debug("zap")
// or create a new one and use it
l := slog.GetLogger()
l.Errorf("default logger name=%s", l.Name())
}Further examples can be seen in the zap_driver_test.go file.
Sometimes it maybe useful to use an instance of the zap.Logger directly. You could use
slogzap.Driver struct for that:
package main
import (
slog "github.com/go-eden/slf4go"
slogzap "github.com/go-eden/slf4go-zap"
"go.uber.org/zap/zaptest"
)
func TestLogger(t *testing.T) {
slog.SetDriver(&slogzap.Driver{
Logger: zaptest.NewLogger(t)
})
// logs will be captured by Go tests
l := slog.GetLogger()
l.Error("report error, without failing the test")
}Only support zap.SugaredLogger, so this library don't have lots of features currently.
zap.Option is now supported.
Hope you can help me improve this library, any Pull Request will be very welcomed.
@phenix3443 @mikeychowy