Skip to content

go-eden/slf4go-zap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slf4go-zap

This is a default Driver implementation for bridging slf4go and zap.

Install

slf4go-zap dependents on slf4go and zap.

go get github.com/go-eden/slf4go-zap

Usage

slf4go-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.

Using zap.Logger directly

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

Notice

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.

Contributor

@phenix3443 @mikeychowy

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages